C# static29 Aug 2024 | 2 min read In 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 |
keyword In the C# programming language, this keyword refers to the current instance of the class. It is mainly utilized to access instance members, such as fields, methods, and properties. It is very essential when there is a naming conflict with numbers. This keyword is...
8 min read
s In the C# programming language, a constructor is a class-specific method that is called automatically whenever an object of that class is created. It holds the set of instructions that are implemented during object creation. The main work of a constructor is to initialize the...
11 min read
In the C# programming language, the Enum is also known as an 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. Enums in C# can be declared within...
12 min read
C# Class and Object In the C# programming language, the class and object are the fundamental components of OOP (Object Oriented Programming). They provide the code in a more organized, reusable, and maintainable way. In C#, classes act like a blueprint that defines how objects are...
10 min read
In the C# programming language, a struct (known as a structure) is like a class that is used to store data under a single unit. Structs are value types, which means that they are stored on the stack and passed by value rather than by...
9 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
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
Destructor in C# In the C# programming language, a destructor is a special method defined in a class that is utilized to perform cleanup operations before an object is removed from memory. A class contains only one destructor in the program. It is prefixed by a...
8 min read
We request you to subscribe our newsletter for upcoming updates.
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