Each one of these instances gives you access to the
members of the class but each instance holds the particular values of the
members of its instance. Consider the results of the following program:
using System;
public class Book
{
public string Title;
public string Author;
public short YearPublished;
public int Pages;
public char CoverType;
}
public class Program
{
static void Main()
{
Book First = new Book();
First.Title = "Psychology and Human Evolution";
First.Author = "Jeannot Lamm";
First.YearPublished = 1996;
First.NumberOfPages = 872;
First.CoverType = 'H';
Console.WriteLine("Book Characteristics");
Console.Write("Title: ");
Console.WriteLine(First.Title);
Console.Write("Author: ");
Console.WriteLine(First.Author);
Console.Write("Year: ");
Console.WriteLine(First.YearPublished);
Console.Write("Pages: ");
Console.WriteLine(First.NumberOfPages);
Console.Write("Cover: ");
Console.WriteLine(First.CoverType);
Book Second = new Book();
Second.Title = "C# First Step";
Second.Author = "Alexandra Nyango";
Second.YearPublished = 2004;
Second.NumberOfPages = 604;
Second.CoverType = 'P';
Console.WriteLine("Book Characteristics");
Console.Write("Title: ");
Console.WriteLine(Second.Title);
Console.Write("Author: ");
Console.WriteLine(Second.Author);
Console.Write("Year: ");
Console.WriteLine(Second.YearPublished);
Console.Write("Pages: ");
Console.WriteLine(Second.NumberOfPages);
Console.Write("Cover: ");
Console.WriteLine(Second.CoverType);
Console.WriteLine();
}
}
This would produce:
Book Characteristics
Title: Psychology and Human Evolution
Author: Jeannot Lamm
Year: 1996
Pages: 872
Cover: H
Book Characteristics
Title: C# First Step
Author: Alexandra Nyango
Year: 2004
Pages: 604
Cover: P
All of the member variables and methods of classes we
have used so far are referred to as instance members because, in
order to access them, you must have an instance of a class declared in
another class in which you want to access them.
C# allows you to declare a class member and refer to
it regardless of which instance of an object you are using. Such a member
variable is called static. To declare a member variable of a class
as static, type the static keyword on its left. Whenever you have a
static member, in order to refer to it, you must "qualify" it in
the class in which you want to call it. Qualifying a member means you must
specify its class. Here is an example:
using System;
public class Book
{
public static string Title;
public static string Author;
public short YearPublished;
public int Pages;
public char CoverType;
}
public class Program
{
static void Main()
{
Book First = new Book();
Book.Title = "Psychology and Human Evolution";
Book.Author = "Jeannot Lamm";
First.YearPublished = 1996;
First.Pages = 872;
First.CoverType = 'H';
Console.WriteLine("Book Characteristics");
Console.Write("Title: ");
Console.WriteLine(Book.Title);
Console.Write("Author: ");
Console.WriteLine(Book.Author);
Console.Write("Year: ");
Console.WriteLine(First.YearPublished);
Console.Write("Pages: ");
Console.WriteLine(First.Pages);
Console.Write("Cover: ");
Console.WriteLine(First.CoverType);
Book Second = new Book();
Book.Title = "C# First Step";
Book.Author = "Alexandra Nyango";
Second.YearPublished = 2004;
Second.Pages = 604;
Second.CoverType = 'P';
Console.WriteLine("Book Characteristics");
Console.Write("Title: ");
Console.WriteLine(Book.Title);
Console.Write("Author: ");
Console.WriteLine(Book.Author);
Console.Write("Year: ");
Console.WriteLine(Second.YearPublished);
Console.Write("Pages: ");
Console.WriteLine(Second.Pages);
Console.Write("Cover: ");
Console.WriteLine(Second.CoverType);
Console.WriteLine();
}
}
Notice that when a member variable has been declared
as static, you don't need an instance of the class to access that member
variable outside of the class. Based on this, if you declare all members
of a class as static, you don't need to declare a variable of their class
in order to access them. In the following example, the Title and Author
fields of the Book class are accessed from the Program class without using
an instance of the Book class:
using System;
public class Book
{
public static string Title;
public static string Author;
}
public class Program
{
static void Main()
{
Book.Title = "Psychology and Human Evolution";
Book.Author = "Jeannot Lamm";
Console.WriteLine("Book Characteristics");
Console.WriteLine("Title: ");
Console.WriteLine(Book.Title);
Console.WriteLine("Author: ");
Console.WriteLine(Book.Author);
Book.Title = "C# First Step";
Book.Author = "Alexandra Miles";
Console.WriteLine("Book Characteristics");
Console.WriteLine("Title: ");
Console.WriteLine(Book.Title);
Console.WriteLine("Author: ");
Console.WriteLine(Book.Author);
Console.ReadLine();
}
}
You can also declare member
variables of the main class as static. If you are referring to a static member variable in
the same class in which it was declared, you don't have to qualify it.
Here is an example:
using System;
public class NewProject
{
static double Length;
static double Width;
static void Main()
{
Console.WriteLine("Rectangles Characteristics");
Length = 22.55;
Width = 20.25;
Console.WriteLine("\nRectangle 1");
Console.Write("Length: ");
Console.WriteLine(Length);
Console.Write("Width: ");
Console.WriteLine(Width);
Length = 254.04;
Width = 408.62;
Console.WriteLine("\nRectangle 2");
Console.Write("Length: ");
Console.WriteLine(Length);
Console.Write("Width: ");
Console.WriteLine(Width);
Console.WriteLine();
}
}
Like a member variable, a method of a class can be defined as
static. This means that this particular method can access any member of the
class regardless of the instance if there are many instances of the class
declared.
To define a method as static, type the static keyword
to its left. 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;
}
static public 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 class Program
{
static void Main()
{
Book.CreateBook();
Book.ShowBook();
}
}
This would produce:
Book Characteristics
Title: Psychology and Human Evolution
Author: Jeannot Lamm
Pages: 472
Price: 24.95
The ReadLine(), the Write(), and the WriteLine()
methods of the Console class that we have used so far are examples of static
methods.
A static class:
- Is a class whose members must be accessed without an instance of the
class. In other words, the members of a static class must be accessed
directly from (using the name of) the class, using the period operator
- Is a class whose members must be created as static. In other words, you
cannot add a non-static member to a static class: all members, except for
constants, must be static
To create a static class, precede the class keyword
with the static keyword. Based on the above two rules, here is an
example:
using System;
namespace Staticity
{
public static class Square
{
public static double Side;
public static double Perimeter()
{
return Side * 4;
}
public static double Area()
{
return Side * Side;
}
}
class Program
{
static void Main(string[] args)
{
Square.Side = 36.84;
Console.WriteLine("Square Characteristics");
Console.Write("Side: ");
Console.WriteLine(Square.Side);
Console.Write("Perimeter: ");
Console.WriteLine(Square.Perimeter());
Console.Write("Area: ");
Console.WriteLine(Square.Area());
}
}
}
This would produce:
Square Characteristics
Side: 36.84
Perimeter: 147.36
Area: 1357.1856
Press any key to continue . . .
|