Readonly , Const & Static Readonly

I am not here mentioning any new thing , just orgainising this; is one of the most frequently asked questions for entry level .net programmers/developers

Const :

1 A Const variable value is initialized at Compile time and Cant be changed afterwards once value is assigned.

2 A Const variable is initiailized at declaration only cant be changed once initialised.

3 It is to be said that Const variable/member is considered as Static by compiler at time of compilation. but Const cant be declared as static.

4 Constants must be of an integral type (sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, decimal, bool, or string), an enumeration,   or a  reference to null.

5  Const cant be of type class / structure bcoz both are initialized at runtime ( with new keyword ) & const expected to assign value at compile time.

e.g.

public class MyClass

{

public const double e = 2.71828;  // Need to assign value at the time of declaration.

}

To use a constant outside of the class that it is declared in, you must fully qualify it using the class name.

*****************************************************************************************************************

Readonly :

1 A Readonly variable can be either instance-level or static.

2 A Readonly variable value is initialized at run time and Cant be changed afterwards once value is assigned.

3 A Readonly variable can be initialized at declaration or by code in the constructor, Can have different values depending upon which constructor is getting  called.(Also variable can be modified/initialized at Static Instructor if Variable is static readonly.)

4 A Readonly members cannot be of type enumerations.

e.g.

public class MyClass

{

public readonly double e = 2.71828;

}

OR

public class MyClass

{

public readonly double e; // Can use Default value ,initialization at the runtime.

public MyClass()

{

e = 2.71828;

}

}

The readonly field can be used for runtime constants as in the following example:

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


REF Details : Readonly &   Static

*****************************************************************************************************************

Static Readonly:

1 We can create ‘static readonly’ member when we want member to be accessed without need to create instance of the class.

2 static readonly is generally used if the type of the field is not allowed in a const declaration, or when the value is not  known at compile t ime.

3 For static readonly member we can modify/initialize at Static Instructor.

Note :

1.  I would like to borrow an examples from MSDN Blogs

Remember that for reference types, in both cases (static and instance) the readonly modifier only prevents you from assigning a new reference to the field.  It specifically does not make immutable the object pointed to by the reference.

class Program

{

public static readonly Test test = new Test();

static void Main(string[] args)

{

test.Name = “Program”;

test = new Test();  // Error: A static readonly field cannot be assigned to (except in a static constructor or a variable initializer)

}

}

class Test

{

public string Name;

}

On the other hand, if Test were a value type, then assignment to test.Name would be an error.

2. If the readonly member is of type int then.

class Test

{

readonly int testValue;

readonly int testnew = 12;

Test(int TestValue)

{

testValue = TestValue;

}

void ChangeYear()

{

testValue = 10; // Will get compile time error.

testnew = 23; // Will get compile time error.

}

}

we  will get the compiler error message:  “The left-hand side of an assignment must be an l-value”

We will get the same error when we try to assign a value to a constant.

Advertisements

Page Validation Properties

Generally used page validation properties while coding.

Page.IsAsync()
Value – true / false
True if the page is processed in asynchronously.

Page.IsCallBack()
Value – true / false
If the Page requested as the result of call back from then true else false

Page.IsCrossPagePostBack()
Value – true / false
CrossPage posting feature in ASP.NET 2.0 allows user to post control values of parent page to target page,we can check parent page is being accessed as a PreviousPage and values are getting posted using IsCrossPagePostBack. For Details – Cross Page Posting

Page.IsPostBack()
Value – true / false
If the page is being loaded for the first time IsPostBack returns false.
If the page is being loaded after some button click / Autopostback event in short any response to a client postback IsPostBack returns true .

Page.IsValid()
Value – true / false
True when page validation is occurred successfully.
We use Page.IsValid if the page contains any of the validation controls.We should generally call Page.IsValid after calling Page.Validate() If we are using Validation group then we should call Page.Validate(“ValidationGroup”) – Validate controls of the ValidationGroup.In ASP.NET 2.0, controls no longer call the Page.Validate method; they use the Page.Validate(String) method instead. If you use the Page.Validate method on an ASP.NET 2.0 page, validation groups are ignored and all controls are validated.

REF MSDN

Also if we set CausesValidation=”true” for server controls then we check it in OnClick event of the controls. Server controls include – Button, ImageButton, and LinkButton Web server controls, The HtmlInputButton, HtmlInputImage, and HtmlButton HTML server controls, and controls that can automatically post back to the server such as the TextBox, CheckBox, ListControl, and BulletedList controls.

Page.IsPostBackEventControlRegistered ( NEW in 3.5)
Gets a value indicating whether the control of the page that submits a postback is registered.