Congratulations to all the winners of April 2013, they have won INR 3400 cash and INR 20147 worth prizes !
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 12400 |  Welcome, Guest!   Register  Login
 Home > Forums > BizTalk Server > difference between Static, read only and Static read only ...
Dipu710646

difference between Static, read only and Static read only

Replies: 2 | Posted by: Dipu710646 on 12/2/2010 | Category: BizTalk Server Forums | Views: 6930 | Status: [Member] | Points: 10  


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


Reply | Reply with attachment | Alert Moderator

 Responses below this adGet hundreds of .NET Tips and Tricks videos

 Replies

T.saravanan
T.saravanan  
Posted on: 12/2/2010 12:04:24 PM
Level: Silver | Status: [Member] [MVP] | Points: 25

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

Sundermagar
Sundermagar  
Posted on: 12/3/2010 2:22:59 AM
Level: Starter | Status: [Member] | 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. | Reply | Alert Moderator 

Reply - Please login to reply


Click here to login & reply

About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/18/2013 2:26:21 AM