static method

In this chapter you will learn:

  1. How to create static methods
  2. Return value from static method
  3. How to intialize value with static methods

Create static methods

A static method can directly call only other static methods, but not an instance method of its class. A static method does not have a this reference.

using System; //from  ja v  a2  s  . c o m
 
class StaticDemo { 
  // a static variable 
  public static int val = 100;  
 
  // a static method 
  public static int valDiv2() { 
    return val/2; 
  } 
} 
 
class MainClass { 
  public static void Main() { 
 
    Console.WriteLine("Initial value of StaticDemo.val is " 
                      + StaticDemo.val); 
 
    StaticDemo.val = 8; 
    Console.WriteLine("StaticDemo.val is " + StaticDemo.val); 
    Console.WriteLine("StaticDemo.valDiv2(): " + 
                       StaticDemo.valDiv2()); 
  } 
}

The code above generates the following result.

Return value from static method

The following code returns the value from a static variable from static function.

using System;/*  j av a2s.  co m*/

class MyClass {
    public MyClass()
    {
        instanceCount++;
    }
    public static int GetInstanceCount()
    {
        return(instanceCount);
    }
    static int instanceCount = 0;
}

class MainClass
{
    public static void Main()
    {
        MyClass my = new MyClass();
        Console.WriteLine(MyClass.GetInstanceCount());
    }
}

The code above generates the following result.

Initialize value with static methods

using System;//from j a  va 2 s  . c  om

public class MyClass
{
   private static int InitX()
   {
      Console.WriteLine( "MyClass.InitX()" );
      return 1;
   }
   private static int InitY()
   {
      Console.WriteLine( "MyClass.InitY()" );
      return 2;
   }
   private static int InitA()
   {
      Console.WriteLine( "MyClass.InitA()" );
      return 3;
   }
   private static int InitB()
   {
      Console.WriteLine( "MyClass.InitB()" );
      return 4;
   }

   private int y = InitY();
   private int x = InitX();

   private static int a = InitA();
   private static int b = InitB();
}

public class MainClass
{
   static void Main()
   {
      MyClass a = new MyClass();
   }
}

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. How to return value from a function
  2. How to return an object value from a method
  3. How to return a value of an array from a 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