Test your technical skills online, appear in Virtual Interview and be ready for job!
Twitter Twitter LinkedIn YouTube Google
Be Interview ready ! | Search
Make friends on DotNetFunda.com and expand your network!
Winners

Win Prizes

Announcements
Like us on Facebook
Sponsor
Top Forums Author
Wed, 23-Apr-2014 Authors
Allemahesh
10
Brierust
10
Klbaiju
10
All Time Authors
Jayakumars
3930
Karthikanbarasan
3455
Chvrsri
3140

Latest members | More ...


(Statistics delayed by 5 minutes)

difference between Static, read only and Static read only

Dipu710646
Posted by Dipu710646 under BizTalk Server on 12/2/2010 9:14:02 AM | Points: 10 | Views : 18850 | Status : [Member] | Replies : 3
What is difference between Static, read only and Static read only variable in C#.



Reply | Reply with Attachment

=> Scroll below for responses


Comments or Responses

Posted by: T.saravanan on: 12/2/2010 [Member] [MVP] Silver | Points: 25

Hi,
Refer those links...

http://en.csharp-online.net/const%2C_static_and_readonly

http://www.allinterview.com/showanswers/33448.html

Thanks,
T.Saravanan

Dipu710646, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sundermagar on: 12/3/2010 [Member] Starter | Points: 25

Hi all,

Here is what I can answer about the above question:

What is difference between Static, read only and Static read only variable in C#.


Static:

Static Variables, Methods or Classes are directly accessible without creating instance of a class as:


public class NonStaticClass
{
//Static method
public static string HelloWorld(string Name)
{
return "Hello" + Name;
}

//Static variable
public static string staticname
{
get;
set;
}
}

Accessing above method and variable

protected void Page_Load(object sender, EventArgs e)
{
//Calling static method
lblNonStatic.Text = NonStaticClass.HelloWorld("sunder");

//Calling static variable
string name = NonStaticClass.StaticVariable;
}

ReadOnly:
In such cases where we want to pre-set the value of the variable which should not be changed by any method further we user ReadOnly for instance:

public class ManageTime
{
//ReadOnly variable
readonly DateTime StartTime;

public ManageTime()
{
this.StartTime = DateTime.Now;
}

//Method Accessing StartTime
public DateTime GetStartTime()
{
//You may want to change the time here. But, it is not allowed due to "ReadOnly" nature of property
return this.StartTime;
}
}

Static ReadOnly:

As we seen above static class does not need instantiation so in the same way static readonly variables are pre-populated and stored in the memory.

Static ReadOnly variable can not be assigned on Initialization because of its static nature.


public class StaticManageTime
{
//ReadOnly variable
static readonly DateTime StartTime=DateTime.Now;

public StaticManageTime()
{
If you will try to assign the value of StartTime in constructor. It will throw an error.

this.StartTime = DateTime.Now;

//<- error "A static readonly field cannot be assigned to (except in a static constructor or a variable initializer)
}

//Method Accessing StartTime
public DateTime GetStartTime()
{
//You may want to change the time here. But, it is not allowed due to "ReadOnly" nature of property
return StartTime;
}
}

Sunder Magar

Dipu710646, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Oliviajones on: 8/19/2013 [Member] Starter | Points: 25

A variable that retains the same data throughout the execution of a program and are accessible without creating instance of a class.

Readonly is the keyword to pre-set the value of the variable which is not changed but whose value we can change during runtime or we can assign it at run time but only through the non-static constructor. Not even by a method.

A Static Readonly type variable's cannot be assigned on Initialization but at runtime this variable's value can only be changed in the static constructor. It can change only once at runtime.
http://www.athenainfotech.co.uk/free-biztalk-health-check


Dipu710646, if this helps please login to Mark As Answer. | Alert Moderator


Login to post response.

Comments / Responses
Select text & click toolbar to format. Formatting appears in viewmode only. HTML Tags are not allowed.
Bold Italic Underline Paragraph Title Code  Link 
 Wait ... Processing ..... please wait.

Found interesting? Add this to: