嫩草视频 两个人的免费视频完整版 午夜视频 好大哥dgdg 骚虎视频 嫩草视频 嫩草视频 骚虎视频 影视大全免费追剧 两个人的免费视频完整版 影视大全 嫩草官网 啦啦啦免费观看视频6 影视大全高清版 野花在线观看免费视频 嫩草视频 骚虎视频入口 免费无限资源 好大哥dgdg 啦啦啦免费观看视频6 啦啦啦免费观看视频6 两个人的免费视频完整版 啦啦啦免费观看视频6 骚虎视频入口 影视大全 啦啦啦免费观看视频6 嫩草视频 野花在线观看免费视频 啦啦啦免费观看视频6 影视大全 影视大全 骚虎视频入口 影视大全免费追剧 24小时日本在线视频 嫩草官网 影视大全免费追剧

How to use static keyword in C# with examples


In this article, I am going to explain the static keyword concept with examples. It covers static variable, static method, static constructor and static class. I have explained how to access static variables and static methods with in a static class.


Static variable:


1. Static keyword can be used with variables, methods, constructor and class.
2. Static variable will be created only once and it can be shared by all objects.
3. If we declare static variable as public, then we can access that variable with classname.static_vairable.
4.Static variable is created when class is loaded in to memory .



class sample
{
public string s="hello";
public static string st="am static";
}
button click
{
sample obj=new sample();
obj.s; //non static variable so can be accessed with object of class.
obj.st; // this is error. since static variable cannot be accessed with object of class.
sample.s; //this is error. since non static variable cannot be called with classname.variablename.
sample.st;// static variable can be accessed with classname.staticvariablename.
}



Static method:

1.Static methods are called with classname.method name directly with out creating object.
2.When we have all static methods in a class, then we can declare that class as static class.
3.Some static methods are as follows
*Console.ReadLine(); // consoleclass.methodname
*Console.Write();
*MessageBox.Show();


class static sample2
{
public static void display()
{
MessageBox.Show("Am from static method");
}
button click
{
sample2 obj=new sample2(); // error . As we cannot create object for static class.
obj.display(); // this is error. cannot be accessed with object.
sample2.display(); //works fine as called with classname.methodname
}



Static constructor:

1.Static constructor is executed only once when the class is loading in to the memory.
2.Static constructor can access only static variables.


class sample3
{
public string s="static constructor example";
public sample3() // normal constructor
{
MesssageBox.Show("Am from normal constructor");
}
static sample3() // static constructor
{
MessageBox.Show("Am from static constructor");
}
}
button click
{
sample3 obj=new sample3();
sample3 obj1=new sample3();
}



Static class:

1. Static class cannot be instantiated, i.e., we cannot create object for static class.
2. If we have all static methods in our class then we can declare the class as static class.



class static sample4
{
public static string s="hello";
public static string st="am static variable";
public static void display()
{
MessageBox.Show("Am from static method");
MessageBox.Show("Am static class");
}
static sample4() // static constructor
{
MessageBox.Show("Am from static constructor");
}
button click
{
sample4.s;
sample4.st;
sample4.display();
}


Comments



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email:
    好大哥影视-好大哥视频-好大哥官网-好大哥电影 骚虎视频官方-房事视频免费看 四虎视频官方-女人本色高清视频 午夜视频官方-24小时日本在线视频 好大哥视频-好大哥dgdg 野花在线官方-影视大全 久草视频入口-嫩草视频官网 小草视频入口-两个人观看野花免费视频 嫩草视频入口-大片免费视频