.Net Framework, Asp.net, Ado.net, .Net Remoting, .Net Webservice, SQL, XML, XSLT, WCF, WPF, WWF NHibernate, Ajax, Jquery, DHTML

Ado.netAsp.Net 2.0SQLC-SharpXMLFrameworkIISXMLWebServiceArchitecture
UMLProject ManagementSDLCMethodologiesDesign PatterenOOPWCF.Net RemotingWWF
MVPMVCSilver LightN-HibernateAjaxJ-QuerySEO-MarketingSite MapPhp

Const vs readonly vs static readonly

There are lots of explanation on net on this topics, I will try to make it as simpler as possible

A constant member is defined at compile time and cannot be changed at runtime. Constants are declared as a field, using the const keyword and must be initialized when they are declared.

Constants can be marked as private, protected, public, internal or protected internal.

Constants are accessed as if they were static fields, although they cannot use the static keyword.

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


public class School
{
public const string address = "http://tech.e-tigers.net";
}

Readonly
the Readonly is also like const, value is not changeable, but the Readonly type programmer can set the value at run time   

public class School
{
public readonly string address;

public MyClass()
{
address = "we are at http://tech.e-tigers.net ";
}
}

Readonly field can be initialized either at the declaration or in a constructor, readonly fields can have different values depending on the number of constructors used

readonly members are not implicitly static, and therefore the static keyword can be applied to a readonly field explicitly

readonly members cannot hold enumerations

Static readonly
We use static modifier to declare a static member, means that the member is no longer tied to a specific object. and the member can be accessed without creating an instance of the class.

Leave your comment.
Name
Email
Comments
Tell Us3580+5 =

Reduce your Dialy IT Cost, Consult with us
Stay healthy in recession

Advertisement
Sponsored by

Privacy Policy ©2009 ETG Consultancy, All Rights Reserved Terms & Conditions
Asp.net, Ado.net, .Net Remoting, .Net Webservice, SQL, XML, XSLT, WCF, WPF, WWF NHibernate, Ajax, Jquery, DHTML