Static keyword can be applied to
Class, field, method, properties, operator, event and constructors.
Static member belongs to the class and not to any object of the class.
They can be used without creating the instance of the class.
For e.g. Static Void Main()
It is called by the operating system on program execution
To access the static member we’ll use
ClassName.staticmember
When a variable is declared as static internally what happens is that all the instances of the class share the same static variable. A static variable is initialized when its class is loaded. If not
initialized explicitly then
it is initialized to zero for numeric variable
null in case of object references
false for boolean
StaticMethod– they can only contain static member and call other static method.
If we need to access them than it can be done through the object of that class
class Game
{
string getGameName()
{
……………….
}
public static void getNameThroughStatic(Game g)
{
g.getGameName(); // accessing static method
}
}
When to use them?
Well we can use them when we need to maintain information applicable to the entire class
suppose we have a class Employees there we can have a static count variable to keep track of no of employees.
class Employees
{
static in count=0;
public Employees()
{
count++;
}
~Employees
{
count–;
}
}
What are Static Constructor?
They can be used to initialize the static variables.
They are called automatically and before the instance constructor (if called any).
for above class
static Employees() // no other access modifiers for them
{}
What are static Classes?
A class whose objects can’t be created and which can only have static members. They can’t be inherited as well.
They can have static constructor
Why use static Classes?
It can be used to group related static method.
thank you this is really useful !
LikeLike
it is a good notes but if u provide more theory ,you can make it batter …
overall nice notes.
sir can you send me interviews question of .net with c# in my e-mail id ?
thankyou ..
LikeLike
hello sir ,pls send me .net c# interview questions..
thank you……
LikeLike
Plz send me asp.net fresher interview questions and answers
LikeLike
these are quite good examples but some of the definations are lacking examples
LikeLike
Very very helping notes sir. Thanks
LikeLike
Thank’s sir i understood about static variable ,class .it is good and best notes for C#.net.plz sir send me .net interview question
LikeLike
I understood all abt static meathod nd variable. Thanx
LikeLike
I understood all abt static variable. Thanx
LikeLike
good
LikeLike
thanks sir.
LikeLike
give more clear idea about “static” keyword
thank you.
LikeLike
Good One
LikeLike
what is that you are saying
public static void getNameThroughStatic(Game g)
{
g.getGameName(); // accessing static method
}
static method can be accessed with Class name. it dont require an instance of class.
LikeLike
Just Awesome way to explain !! Thanks !!
LikeLike
class Game
{
void string getGameName()
{
……………….
}
public static void getNameThroughStatic(Game g)
{
g.getGameName(); // accessing static method
}
}
Code and definition posted above for static method is wrong. We can’t create object of static class and cant access static method by creating object of a class.
In above code, there is lot of mistake..
1st, Two return type cant be declared for a single method and 2nd we can’t access static method by creating object of a class. It should be accessed by class name like
Game.getGameName(); instead of g.getGameName();
Please check your code thoroughly before posting.
LikeLike
Hurrah! Finally I got a blog from where I be able to in fact get helpful data regarding my study and knowledge.
LikeLike
I visited many web sites except the audio quality for audio songs current at this site is in
fact excellent.
LikeLike