Javatpoint.com is changed to TpointTech.com
C# staticIn C#, static is a keyword or modifier that belongs to the type not instance. So instance is not required to access the static members. In C#, static can be field, method, constructor, class, properties, operator and event. Note: Indexers and destructors cannot be static.Advantage of C# static keywordMemory efficient: Now we don't need to create instance for accessing the static members, so it saves memory. Moreover, it belongs to the type, so it will not get memory each time when instance is created. C# Static FieldA field which is declared as static, is called static field. Unlike instance field which gets memory each time whenever you create object, there is only one copy of static field created in the memory. It is shared to all the objects. It is used to refer the common property of all objects such as rateOfInterest in case of Account, companyName in case of Employee etc. C# static field exampleLet's see the simple example of static field in C#. Output: 101 Sonoo 8.8 102 Mahesh 8.8 C# static field example 2: changing static fieldIf you change the value of static field, it will be applied to all the objects. Output: 101 Sonoo 10.5 102 Mahesh 10.5 C# static field example 3: Counting ObjectsLet's see another example of static keyword in C# which counts the objects. Output: 101 Sonoo 102 Mahesh 103 Ajeet Total Objects are: 3 Next TopicC# static class |
Javatpoint.com is now changed to TpointTech.com, so we request you to subscribe our newsletter for further updates.
is used to initialize static fields. It can also be used to perform any action that is to be performed only once. It is invoked automatically before first instance is created or any static member is referenced. Points to remember for C# Static Constructor cannot have...
2 min read
Enum in C# is also known as enumeration. It is used to store a set of named constants such as season, days, month, size etc. The enum constants are also known as enumerators. Enum in C# can be declared within or outside class and structs. Enum constants...
3 min read
In C#, constructor is a special method which is invoked automatically at the time of object creation. It is used to initialize the data members of new object generally. The constructor in C# has the same name as class or struct. There can be two types of...
3 min read
Since C# is an object-oriented language, program is designed using objects and classes in C#. C# Object In C#, Object is a real world entity, for example, chair, car, pen, mobile, laptop etc. In other words, object is an entity that has state and behavior. Here, state means data...
5 min read
The is like the normal class but it cannot be instantiated. It can have only static members. The advantage of static class is that it provides you guarantee that instance of static class cannot be created. Points to remember for contains only static members. cannot...
1 min read
In C#, classes and structs are blueprints that are used to create instance of a class. Structs are used for lightweight objects such as Color, Rectangle, Point etc. Unlike class, structs in C# are value type than reference type. It is useful if you have data that...
2 min read
A destructor works opposite to constructor, It destructs the objects of classes. It can be defined only once in a class. Like constructors, it is invoked automatically. Note: C# destructor cannot have parameters. Moreover, modifiers can't be applied on destructors. C# Constructor and Destructor Example Let's see an example...
1 min read
In c# programming, this is a keyword that refers to the current instance of the class. There can be 3 main usage of this keyword in C#. It can be used to refer current class instance variable. It is used if field names (instance variables) and parameter...
2 min read
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India