

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What are static members of a C# Class?
We can define class members as static using the static keyword. When we declare a member of a class as static, it means no matter how many objects of the class are created, there is only one copy of the static member.
The keyword static implies that only one instance of the member exists for a class. Static variables are used for defining constants because their values can be retrieved by invoking the class without creating an instance of it. Static variables can be initialized outside the member function or class definition. You can also initialize static variables inside the class definition.
The following is an example −
Example
using System; namespace StaticVarApplication { class StaticVar { public static int num; public void count() { num++; } public int getNum() { return num; } } class StaticTester { static void Main(string[] args) { StaticVar s1 = new StaticVar(); StaticVar s2 = new StaticVar(); s1.count(); s1.count(); s1.count(); s2.count(); s2.count(); s2.count(); Console.WriteLine("Variable num for s1: {0}", s1.getNum()); Console.WriteLine("Variable num for s2: {0}", s2.getNum()); Console.ReadKey(); } } }
Output
Variable num for s1: 6 Variable num for s2: 6
- Related Questions & Answers
- What are static members of a Java class?
- When are static C++ class members initialized?
- What are the steps to read static members in a Java class?
- Static Data Members in C++
- Defining static members in C++
- What are the differences between class methods and class members in C#?
- What are the differences between a static and a non-static class in C#?
- What are static methods in a Python class?
- What is a static class in C#?
- What are Class/Static methods in Java?
- Static Members in Ruby Programming
- What is a non-static class in C#?
- How to initialize private static members in C++?
- Accessing protected members in a C++ derived class
- What is a static storage class in C language?
Advertisements