Constants vs. Read-Only Fields

Tuesday, 22 June 2010 23:15 by Harpreet

Read this article in your language IT | EN | DE | ES

Constants:
Constants are fields that remain constant for the life of the application. It is represented by the const keyword in c#. Constant is a member whose value is set at compile time, either by the programmer or defaulted by the compiler. One point to remember that there's no need for the client to instantiate the class because by default const members are static.

using System;
class A
{
    public const double pi = 3.1415;
    public const int answerToAllLifesQuestions = 42;
}

class ConstApp
{
    public static void Main()
    {
        Console.WriteLine("pi = {0}, everything else = {1}", A.pi, A.answerToAllLifesQuestions);
    }
}

Read-Only Fields:
A field defined as a const is useful because it clearly documents the programmer's intention that the field contains an immutable value. However, that only works if you know the value at compile time. So what does a programmer do when the need arises for a field with a value that won't be known until run time and should not be changed once it's been initialized?

When you define a field with the readonly keyword, you have the ability to set that field's value in one place: the constructor. After that point, the field cannot be changed by either the class itself or the class's clients. Let's say you want to keep track of the screen's resolution for a graphics application. You can't address this problem with a const because the application cannot determine the end user's screen resolution until run time, so you use code like that at the top of the following page.

using System;
class GraphicsPackage
{
    public readonly int ScreenWidth;
    public readonly int ScreenHeight;
    public GraphicsPackage()
    {
        this.ScreenWidth = 1024;
        this.ScreenHeight = 768;
    }
}

class ReadOnlyApp
{
    public static void Main()
    {
        GraphicsPackage graphics = new GraphicsPackage();
        Console.WriteLine("Width = {0}, Height = {1}", graphics.ScreenWidth, graphics.ScreenHeight);
    }
}

At first glance, this code seems to be just what you would need. However, there's one small issue: the read-only fields that we defined are instance fields, meaning that the user would have to instantiate the class to use the fields. This might not be a problem and could even be what you want in cases in which the way the class is instantiated will determine the read-only field's value. But what if you want a constant, which is static by definition that can be initialized at run time? In that case, you would define the field with both the static and the readonly modifiers. Then you'd create a special type of constructor called a static constructor. Static constructors are constructors that are used to initialize static fields, read-only or otherwise. Here I've modified the previous example to make the screen resolution fields static and read-only, and I've added a static constructor. Note the addition of the static keyword to the constructor's definition:

using System;
class GraphicsPackage
{
    public static readonly int ScreenWidth;
    public static readonly int ScreenHeight;

    static GraphicsPackage()
    {
        ScreenWidth = 1024;
        ScreenHeight = 768;
    }
} 

class ReadOnlyApp
{
    public static void Main()
    {
        Console.WriteLine("Width = {0}, Height = {1}", GraphicsPackage.ScreenWidth, GraphicsPackage.ScreenHeight);
    }
}

Categories:   OOPS
Actions:   E-mail | Permalink | Comments (1) | Comment RSSRSS comment feed

Comments

November 3. 2010 09:52

online casino guide

pre-enthusiastic

online casino guide

About me

Chat with Admin

Gmail: dotnetcracknews@gmail.com
Mobile: +91-9871115233

Calendar

<<  May 2012  >>
MoTuWeThFrSaSu
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

View posts in large calendar
Disclaimer

The articles documented here contain some material excerpted from net, I will try to make sure that I mention reference to those sites, but if I miss out on some areas it is not intentional.

© Copyright 2012