C# Classes Explained - Static members
(Page 6 of 6 )
A Static member is a member that has a class-level access, unlike instance members which must be called on an instance of the class. To call a Static member, simply use the class name followed by the (.) operator without creating an object from the class. Static members are useful for many scenarios.
Take the Console class as an example; to write a line to the console you use the static method WriteLine() of the Console class. In this scenario, it doesn't make sense to create an instance of the Console class and call WriteLine(), because all of the objects would write a line to the Console Window.
You can use static members to provide functionality that is not bound to an instance of the object; a class like Console can't be instantiated, and all its members are static. This is the perfect scenario, where we have some functionality and code that doesn't fit in the Object Oriented Programming Objects and Classes world, such as the Console class.
If you have a class that contains a static field and another instance field, note that each time you create an instance of the class, the CLR will allocate a memory location for the instance field, but, as for the static field, it will create only one memory location no matter how many objects you have. Think of the static members as class level members, and the instance members as per object data fields. Also remember that a static method can only manipulate static fields; we will discuss this issue in the methods article.
Turning back to our Employee class example, suppose that we need some way to track how many employee objects we have. Look at the following code:
using System;
namespace MyCompany
{
public class Employee
{
public decimal Salary;
public const int MaxOverTimeHours = 10;
public static double EmployeeCounter;
public Employee()
{
EmployeeCounter++;
}
}
public class EmployeeTest
{
static void Main(string[] args)
{
Employee Michael = new Employee();
Employee Youssef = new Employee();
Employee David = new Employee();
Console.WriteLine(Employee.EmployeeCounter);
Console.ReadLine();
}
}
}
The result of this code is 3. In the Employee class we added a public static field called EmployeeCounter, and in the default constructor of the Employee class (for now think of a constructor as a special type of methods that is called every time you create an instance of your class), we increase the EmployeeCounter by 1 for each new employee instance. In the EmployeeTest class we created 3 objects of type Employee, and printed the value of the EmployeeCounter field. Note that we used the class name in order to access the static field.
In the next article, we will discuss control C# program structure.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |