none
Difference Between Constant, Readonly and Static Variables

    Question

  • Hi all,

    Can Anybody tell me what is the Exact Difference Between Constant, Readonly and Static Variables...

    Thanks

    Monday, September 06, 2010 10:48 AM

Answers

  • hi,

     

    please read:

    http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/7c6cfa22-b3aa-44b1-9567-13ba9259d9c7


    BR, Karol. mark as answer/vote as helpful if it helped you
    Monday, September 06, 2010 10:52 AM
  • Hi WolverineV9,

     

    Welcome to MSDN Form!

     

    const (C# Reference)

    The const keyword is used to modify a declaration of a field or local variable. It specifies that the value of the field or the local variable is constant, which means it cannot be modified.

     

    readonly (C# Reference)

    The readonly keyword is a modifier that you can use on fields. When a field declaration includes a readonly modifier, assignments to the fields introduced by the declaration can only occur as part of the declaration or in a constructor in the same class.

     

    Static variable

    It is used to restore the data for sharing to all instances of this type.

     

    If there’s anything unclear, please feel free to let me know. And I’m glad to help you.

     

    Have a nice day!

    Mike

    -------------------------------------------------------------------

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to the others community members reading the thread.

    Wednesday, September 08, 2010 6:28 AM

All replies

  • hi,

     

    please read:

    http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/7c6cfa22-b3aa-44b1-9567-13ba9259d9c7


    BR, Karol. mark as answer/vote as helpful if it helped you
    Monday, September 06, 2010 10:52 AM
  • Hi WolverineV9,

     

    Welcome to MSDN Form!

     

    const (C# Reference)

    The const keyword is used to modify a declaration of a field or local variable. It specifies that the value of the field or the local variable is constant, which means it cannot be modified.

     

    readonly (C# Reference)

    The readonly keyword is a modifier that you can use on fields. When a field declaration includes a readonly modifier, assignments to the fields introduced by the declaration can only occur as part of the declaration or in a constructor in the same class.

     

    Static variable

    It is used to restore the data for sharing to all instances of this type.

     

    If there’s anything unclear, please feel free to let me know. And I’m glad to help you.

     

    Have a nice day!

    Mike

    -------------------------------------------------------------------

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to the others community members reading the thread.

    Wednesday, September 08, 2010 6:28 AM
  • Hi to all:

    I have find out some following reasons for differents between static,constant and readonly.

    Static Variable:

    static variable is class variable.it means u can able to call tha variable using class name.And it can be able to change the value.

    ex:

    public class test

    {

    public static int s=10;

    static void Main(string[] args)

    {

    Console.writeline(test.s);

    //output 10

    then u can able to change that value

    test.s=20;//call the static variable throw classname

    console.writeline(test.s);

    //out put 20

    }

    }

    Read only

    Read only variables can't be changed with in the method,but we can change with in the constructor

    Ex:

    public class test

    {

    public readonly int R=10;

    puablic void normalmethd() //Method

    {

    R=5;//This throw the error because can't be changed read only variable with in the method

    }

    public test()//Constructor

    {

    R=5;This is possible to cange the value with in the constructor

    }

    static void Main(string[] args)

    {

    test obj=new test();

    //out put 20

    }

    }

    Constant:

    Once u Declare the constant variable that variable cant be changed

    Wednesday, March 28, 2012 2:07 PM
  • Old thread is old.

    It is not beneficial to the community to answer 1.5 year old questions, especially questions with multiple correct answers.  You should spend your time focusing on unanswered questions asked within the past few days.

    Wednesday, March 28, 2012 2:43 PM