Static Class

In this chapter you will learn:

  1. Create and use static class
  2. How to use static class to create utility class

Usage for static class

Classes (but not structs) can be declared as static. A static class can contain only static members and cannot be instantiated with the new keyword.

One copy of the class is loaded into memory when the program loads, and its members are accessed through the class name.

Both classes and structs can contain static members.

A static class can contain only static members. We cannot instantiate the class using a constructor. We must refer to the members through the class name.

The following code defines a static class to store message.

using System;//j a  va 2  s  .  c o  m

public static class MyStaticClass
{

    public static string getMessage()
    {
        return "This is a static member";
    }

    public static string StaticProperty
    {
        get;
        set;
    }

}

public class MainClass
{
    static void Main(string[] args)
    {
        Console.WriteLine(MyStaticClass.getMessage());
        MyStaticClass.StaticProperty = "this is the property value";
        Console.WriteLine(MyStaticClass.StaticProperty);

    }
}

The output:

static utility class

The following code shows how to use static class to create utility class. The utility class is for math calculation.

using System;  /*from   j  a  v a 2 s. c  om*/
  
static class MathFunction {  
  // Return the reciprocal of a value. 
  static public double reciprocal(double num) { 
    return 1/num; 
  } 
 
  // Return the fractional part of a value. 
  static public double fracPart(double num) { 
    return num - (int) num; 
  } 
 
  // Return true if num is even. 
  static public bool isEven(double num) { 
    return (num % 2) == 0 ? true : false; 
  } 
 
  // Return true of num is odd. 
  static public bool isOdd(double num) { 
    return !isEven(num); 
  } 
 
}  
 
class MainClass {  
  public static void Main() {    
    Console.WriteLine("Reciprocal of 5 is " + 
                      MathFunction.reciprocal(5.0)); 
 
    Console.WriteLine("Fractional part of 4.234 is " + 
                      MathFunction.fracPart(4.234)); 
 
    if(MathFunction.isEven(10)) 
      Console.WriteLine("10 is even."); 
 
    if(MathFunction.isOdd(5)) 
      Console.WriteLine("5 is odd."); 
 
    // The following attempt to create an instance of  
    // MathFunction will cause an error. 
//  MathFunction ob = new MathFunction(); // Wrong! 
  }  
}

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. How to create partial type
  2. Partial method
Home » C# Tutorial » Class
Classes and Structs
Encapsulation
Class and Struct Accessibility
Members
Static Members
Class
Class instance variables
Class instance Methods
Class Inheritance
Class constructor
Default constructors
Parameters of constructors
Constructor overloading
Static Constructor
Destructor
Override destructor
new operator
Class field
Static Fields
Read only field
Field default value
const value
Abstract class
Static Class
Partial type
Methods
Method local variable
Method Overloading
Method overloading with ref and out
Type conversion and method overloading
Error in Method overloading
virtual methods
Override methods
Method Parameter
Pass parameters
ref parameters
out parameters
Variable parameter length
Optional parameters
Named parameters
static method
function Return
Recursive methods
Virtual methods and polymorphism
Cast
Shadow inherited members
Seal a member
this keyword
base keyword
base and name hiding
Access Specifiers
private
public
protected
internal
Indexer
String type indexer
Multidimensional indexer
Indexer overloading
Indexer to append
Indexer logic
Properties
Properties getter and setter
Accessibilities of properties
Automatic properties
Read only and write only property
Virtual properties
Abstract Properties
Static Properties
interface
interface implementation
Explicit interface implementation
Interface inheritance
Indexer in an interface
Interface Properties
struct and interface
virtual interface implementation
as operator
is operator
null reference
Nested classes
object class
ToString method
GetHashCode
Object initialization
Extension method
Struct Instances vs. Class Instances