Guruji Point - Code You Want To Write

  • Home
  • Asp.Net
  • WCF
  • SQL
  • Type Script
  • AngularJS
  • Tech News
  • Blog
  • Earn Online

Wednesday, 20 April 2016

Const Readonly and Static Keyword With Example

 asp.net, Asp.Net C#, asp.net interview questions     No comments   

Const


By using const keyword you can define a constant field. Constants can be numbers, Boolean values, strings, or a null reference. Constant fields may not be modified. Constant value can not be change or modified with in the programme once it is allocated to a const field.
You can initialize const field at the time of declaration otherwise it will produce error (" The left-hand side of an assignment must be a variable, property or indexer"). That's why const field is like Compile Time Constant.

Example:

  public class ConstExample
    {
        class SampleClass
        {
            public int x;
            public int y;
            public const int c1 = 5;
            public const int c2 = c1 + 5;

            public SampleClass(int p1, int p2)
            {
                x = p1;
                y = p2;
               // c1 = 6;             //Compile-Time Error
            }
        }

        static void Main()
        {
            SampleClass objClass = new SampleClass(15, 20);
            Console.WriteLine("x = {0}, y = {1}", objClass .x, objClass .y);
            Console.WriteLine("c1 = {0}, c2 = {1}",
            SampleClass.c1, SampleClass.c2);
        }
    }
    /* Output
        x = 15, y = 20
        c1 = 5, c2 = 10

Readonly

By using readonly keyword you can define a readonly field. you can define values for your readonly field either at the time of declaration or with in constructor.  this is the only difference between readonly and const keyword.

Note:  Const field can only be initialized at the declaration of the field but readonly can be initialized either at the declaration or in a constructor. Therefore, readonly fields can have different values depending on the constructor used
Example:--

class ReadonlyExample
   {
      public int x;
      // Initialize a readonly field at declaration
      public readonly int y = 15;
      public readonly int z;

      public ReadonlyExample()
      {
         // Initialize a readonly instance field in constructor
         z = 24;
      }

      public ReadonlyExample(int p1, int p2, int p3)
      {
         x = p1;
         y = p2;
         z = p3;
      }
   }

   static void Main()
   {
      ReadonlyExample p1 = new ReadonlyExample(11, 21, 32);   // OK
      Console.WriteLine("p1: x={0}, y={1}, z={2}", p1.x, p1.y, p1.z);
      ReadonlyExample p2 = new ReadonlyExample();
      p2.x = 55;   // OK
      Console.WriteLine("p2: x={0}, y={1}, z={2}", p2.x, p2.y, p2.z);
   }
}

In above console examle you can see i initialize my readonly field in two ways . One is declaration time and other is within constructor.

Static

Static means that only one instance of a given variable exists for a class.Static value field initialize when programme  execution start and its scope remains in the programme until the execution or programme stop.  
Static variables are used to define constants because their values can be retrieved by invoking the class without creating an instance of it. The static modifier can be used with classes, fields, methods, properties, operators, events, and constructors.
Note:  Static value field same as constants but we can modify or perform operation on static field value like increment and decrement which we do not perform with const.

Example:
public  class StaticExample
{
    public static int i = 5;
    public void test()
    {
        i=i+5;
        Console.WriteLine(i);
    }
}


public class Demo
{
    static void Main()
    {
        StaticExample var = new StaticExample();       
        var.test();                                     // value of i=10   ( i=i+5)
        StaticExample var1 = new StaticExample();
        var1.test();                                    //value of i=15
        Console.ReadKey();
    }
}
 in this example if you take a no static variable like
 public int i = 5 it will print 10 both the times. because non-static variable lots its scope when new object creates, In other words it will initialize to zero after the current object execution stops.
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Email ThisBlogThis!Share to XShare to Facebook
Newer Post Older Post Home

0 comments:

Post a Comment

Facebook Updates

Guruji Point

Categories

Comman table Expression in SQL Dynamic Grid Row IQueryable in c# Learn AngularJS Scope_Identity() Use of indexes comma seperated string in sql row_number() and dense_rank() salary table structure. while loop in sql why do we need cursor why tuple used

About Us

This blog is about the Programming ,Tech News and some intresting facts related Contents. Here you can find fundamental and expert level topic of programming languages very practically related to C# , Asp.Net ,Sql-Server, JavaScript, Jquery, WebServices And also Web-Api, WCF ,WPF, Angular Js.

Contact Us

Email Us - gurujipoints@gmail.com
Contact- 8077657477

Reach Us

Copyright © Guruji Point - Code You Want To Write