DIFFERENCE BETWEEN STATIC AND NON-STATIC CONSTRUCTOR
There are
some important point about static and non-static constructor it cover the definition,
advantages and differences.
1. The entire constructor that explicitly defined by the keyword
static will be static constructor and the rest of all constructor will
non-static constructor.
2. Static constructor never defined implicitly it always defined explicitly
by programmer as needed. Where the non-static constructor such as default/parameter-less
constructor defined implicitly, if non-static constructor does not define
explicitly.
3. If the variable in class is static then it always initialize by
static constructor, non- static constructor never initialize the static variable
or static instance variables. Non-static constructor responsible to initialize the
non-static variable/field of the class.
4. Static constructor are implicitly called if we does not call in
main class even then it execute immediately after class execution start, where
the non-static constructor execute if and only if we called it by instantiating
its object and execute each and every time after instantiating the class.
5. Static constructor always execute before executing the code of
main class but even then the main class will be the entry point of project not
the static members, because static member does not to instantiating the class
object to execute the member. Where the non-static constructor and member always
execute after the instantiating/creating the class object.
6. Static constructor execute only for one time in life cycle of a program
, where the non-static constructor execute 0 times if the instance does not
created otherwise it execute each and every time the class instance are
created.
7. Static constructor never is parameterized it must be parameter less,
because the static constructor implicitly called and always has fixed values
and that are declare in that class by static constructor. Whereas the non-static
constructor can be parameterized.
8. Static member can directly access with in a static block. But
the non-static always access using class instance.
9. Static constructor never overloaded, because overloading is done
by parameter where static constructor are parameter less constructor. But we
can perform overloading in non-static constructor because it can be parameterized.
10. All the class having implicit constructor, here the static
constructor is implicitly defied if and only if the class having any static
field, otherwise all the implicit constructor of any class will be non-static
implicit constructor.
difference between static and non-static constructor c# |
0 comments:
Post a Comment
Please do not enter any spam link in the comment box