• USA : +1 973 910 5725
  • INDIA: +91 905 291 3388
  • info@tekslate.com
  • Login

Static Keyword in C# .Net

Working with Static Keyword

Class Circle

{

Private int x, y, z;

Private Static double pi = 3.14;.

}

Static is a keyword.

Static keyword can be used with fields, constructors, methods and classes.

Static Fields will be created only once while class is loaded in to the memory.

Instance Variables will be created separately with every object.

Static variables creates common memory which is sharable by all the objects.

If Static variable is public, then it can be accessed directly with class name But NOT WITH OBJECTS.

Observations

Class Test

{

Public int I;

Public Static int S;

}

Test I = 10; wrong

Test s =20; correct

Test t = new Test ()

  1. I = 10; correct

t.s = 20;wrong

 

Static constructor can access only static variables.

Static constructor will be Executed only once, While class is loading in to the memory.

 

Example on static variables and static constructor

Open windows forms Application Project arrrowarrrowPlace a button arrrowCode in GD

Class Test

{

Private int I;

Private Static int s;

Static Test ()// Static constructor

{

S= 0;

}

Public Test ()//Normal Constructor

{

I = I +1;

S = s+1;

Message Box>show  (I + “” +s);

}

}//Test

 

Code for button 1_ click

{

Test t1 = new Test ();

Test t2 = new Test ();

Test t3 = new Test ();

}

 

S=0

T1  I =0

I=0 +1

I =1

S=1

 

I          s

1         1

 

 

T2   I =0

I = 0+1

I =1

S =s +1

S =I +1

S= 2

 

I     s

I     2                t2

 

I     s

I     3                  t3

 

Onservations

 

Class JA  (Joint account)

{

Private string Name ()

 

Screenshot_45

 

Static Method and Static Classes

 

Class Test

{

Public Static void P1 (){}

Public Static void P2 (){}

}

 

Static method can access only Static data.

Static method need to be called with class name.

 

 

Screenshot_46

 

When a class contains all static methods only. Then recommended to declare that class as static.

Static class are not intangible (creation of object is not allowed) 

 

Example on static method and static classes

Open windows Application Project àPlace a text box and a button

Note

 

arrrowIn this program a predefined class called as process is  required.

arrrowProcess class is a  part of System. Diagnostics namespace.

arrrowIn order to use process classes, this namespace need to be imported with the help of “using”  keyword.

arrrowCode in GD

Static classes software

{

Public static void open (string s)

{

Process. start (s);

}//open

}//software class

 

arrrowcode for Button 1-cilck

{

Software. open (text Box1.text);

}

 

“At TekSlate, we are trying to create high quality tutorials and articles, if you think any information is incorrect or want to add anything to the article, please feel free to get in touch with us at info@tekslate.com, we will update the article in 24 hours.”

0 Responses on Static Keyword in C# .Net"

    Leave a Message

    Your email address will not be published. Required fields are marked *

    Site Disclaimer, Copyright © 2016 - All Rights Reserved.

    Support


    Please leave a message and we'll get back to you soon.

    I agree to be contacted via e-mail.