Home

C# Topics: Static Methods

     

Introduction

Like a member variable, a method of a class can be defined as static. Such a method can access any member of the class but it depends on how the member variable was declared. Remember that you can have static or non-static members of a class.

   

Creating a Static Method

To define a method as static, type the static keyword to its left. Here is an example:

public class Book
{
    static void CreateBook()
    {
	
    }
}

A static method can also use an access modifier. You can write the access modifier before or after the static keyword. Here is an example:

using System;

public class Book
{
    private static string title;
    static private string author;
    private static int    pages;
    static private double price;

    static public void CreateBook()
    {
	title  = "Psychology and Human Evolution";
	author = "Jeannot Lamm";
	pages  = 472;
	price  = 24.95;
    }

    internal static void ShowBook()
    {
	Console.WriteLine("Book Characteristics");
	Console.Write("Title:  ");
	Console.WriteLine(Book.title);
	Console.Write("Author: ");
	Console.WriteLine(Book.author);
	Console.Write("Pages:  ");
	Console.WriteLine(pages);
	Console.Write("Price:  ");
	Console.WriteLine(price);
    }

    public static int Main()
    {
	return 0;
    }
}

As mentioned for a static field, using a static method depends on where it is being accessed. To access a static member from a static method of the same class, you can can just use the name of the static member. Here are examples:

using System;

public class Book
{
    static string title;
    static string author;
    static int pages;
    static double price;

    static void CreateBook()
    {
        title = "Psychology and Human Evolution";
        author = "Jeannot Lamm";
        pages = 472;
        price = 24.95;
    }

    static void ShowBook()
    {
        Console.WriteLine("Book Characteristics");
        Console.Write("Title:  ");
        Console.WriteLine(Book.title);
        Console.Write("Author: ");
        Console.WriteLine(Book.author);
        Console.Write("Pages:  ");
        Console.WriteLine(pages);
        Console.Write("Price:  ");
        Console.WriteLine(price);
    }

    public static int Main()
    {
        CreateBook();
        ShowBook();
        
        return 0;
    }
}

This would produce:

Book Characteristics
Title:  Psychology and Human Evolution
Author: Jeannot Lamm
Pages:  472
Price:  24.95

You can also type the name of the class, followed by a period, followed by the member. Here are examples:

using System;

public class Book
{
    . . . No Change

    static void CreateBook()
    {
        . . . No Change
    }

    static void ShowBook()
    {
        . . . No Change
    }

    public static int Main()
    {
        Book.CreateBook();
        Book.ShowBook();
        
        return 0;
    }
}

To access a static member outside of its class, type the name of the class, followed by a period, followed by the member. Here are examples:

using System;

public class Book
{
    . . . No Change

    static public void CreateBook()
    {
        . . . No Change
    }

    internal static void ShowBook()
    {
        . . . No Change
    }
}

public class Exercise
{
    public static int Main()
    {
        Book.CreateBook();
        Book.ShowBook();

        return 0;
    }
}
 
 

Home Copyright © 2010-2011 FunctionX