Pages
    Calender
    <<  March 2010  >>
    MoTuWeThFrSaSu
    22232425262728
    1234567
    891011121314
    15161718192021
    22232425262728
    2930311234
    Ajander Singh , Created On 12. November 2008, 20:07

    The tradition of using Constants haven't changed much through the times of C++ and now C#, the implication is also the same. Looking deep into the working of Constatnts in C#, we can summarize that A constant is a symbol that has a never-changing value and when declaring a constant symbol. The value of constant as known as compile time and do not change, the compiler then saves the constant's value in the assembly's metadata. This means that you can define a constant only for t primitive types. In C#, the following types are primitives and can be used to define constants: Boolean, Char, Byte, SByte, Int16, UInt16, Int32, UInt32, Int64, UInt64, Single, Double, Decimal, and String.

    Constants are declared  as a field, using the const keyword before the type of the field. Constant must be initilized as they are declared because a constant are always considered to be part of defining type. Constant can be marked as public, private, protected, internal or protected internal. these access modifiers defines how users of the class can access constant.

    Note: Constant are always considerd to be static memberes not instance members. Definig a constant causes the creation of metadata. 

    Example:

    class Calendar

    {

        const int months = 12;

        const int weeks = 52;

        const int days = 365;

     

        const double daysPerWeek = days / weeks;

        const double daysPerMonth = days / months;

    Constants are accessed as if they were static fields, although they cannot use the static keyword. Expressions that are not contained within the class defining the constant must use the class name, a period, and the name of the constant to access the constant. For example:

    int birthstones = Calendar.months; 

    When code refer to const keyword or constant sysmbol, compiler loop up the sysmbol in the metadata of the assembly that defines the constant, extract the constant's value, and ambed value in the emitted IL code. Because a constant's value is embedded directly in code, constants don't require any memory to be allocated for them at run time. In addition, you can't get the address of a constant and you can't pass a constant by reference. These constraints also mean that constants don't have a good cross-assembly versioning story, so you should use them only when you know that the value of a symbol will never change.



    Currently rated 1.0 by 1 people

    • Currently 1/5 Stars.
    • 1
    • 2
    • 3
    • 4
    • 5
    Admin , Created On 10. November 2008, 20:48

    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. Compilers and verification ensure that readonly fields are not written to by any method other than a constructor. Note that reflection can be used to modify a readonly field

    In this example, the value of the field year cannot be changed in the method ChangeYear, even though it is assigned a value in the class constructor:

    class Age

    {

        readonly int _year;

        Age(int year)

        {

            _year = year;

        }

        void ChangeYear()

        {

            _year = 1967; // Will not compile.

        }

    }

    You can assign a value to a readonly field only in the following contexts:

    • When the variable is initialized in the declaration, for example:
      public readonly int y = 5;
    • For an instance field, in the instance constructors of the class that contains the field declaration, or for a static field, in the static constructor of the class that contains the field declaration. These are also the only contexts in which it is valid to pass a readonly field as an out or ref parameter.

    The readonly keyword is different from the const keyword. A const field can only be initialized at the declaration of the field. A readonly field can be initialized either at the declaration or in a constructor. Therefore, readonly fields can have different values depending on the constructor used. Also, while a const field is a compile-time constant, the readonly field can be used for runtime constants as in the following example:

    public static readonly uint l1 = (uint)DateTime.Now.Ticks;

    Also, in C#, there are some performance issues to consider when initializing fields by using inline syntax versus assignment syntax in a constructor. 



    Be the first to rate this post

    • Currently 0/5 Stars.
    • 1
    • 2
    • 3
    • 4
    • 5