Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
CSharpNotesForProfessionals.pdf
Скачиваний:
57
Добавлен:
20.05.2023
Размер:
6.12 Mб
Скачать

Chapter 42: Static Classes

Section 42.1: Static Classes

The "static" keyword when referring to a class has three e ects:

1.You cannot create an instance of a static class (this even removes the default constructor)

2.All properties and methods in the class must be static as well.

3.A static class is a sealed class, meaning it cannot be inherited.

public static class Foo

{

//Notice there is no constructor as this cannot be an instance public static int Counter { get; set; }

public static int GetCount()

{

return Counter;

}

}

public class Program

{

static void Main(string[] args)

{

Foo.Counter++;

Console.WriteLine(Foo.GetCount()); //this will print 1

//var foo1 = new Foo();

//this line would break the code as the Foo class does not have a constructor

}

}

Section 42.2: Static class lifetime

A static class is lazily initialized on member access and lives for the duration of the application domain.

void Main()

{

Console.WriteLine("Static classes are lazily initialized");

Console.WriteLine("The static constructor is only invoked when the class is first accessed"); Foo.SayHi();

Console.WriteLine("Reflecting on a type won't trigger its static .ctor"); var barType = typeof(Bar);

Console.WriteLine("However, you can manually trigger it with System.Runtime.CompilerServices.RuntimeHelpers");

RuntimeHelpers.RunClassConstructor(barType.TypeHandle);

}

// Define other methods and classes here public static class Foo

{

static Foo()

{

Console.WriteLine("static Foo.ctor");

}

public static void SayHi()

GoalKicker.com – C# Notes for Professionals

175

{

Console.WriteLine("Foo: Hi");

}

}

public static class Bar

{

static Bar()

{

Console.WriteLine("static Bar.ctor");

}

}

Section 42.3: Static keyword

The static keyword means 2 things:

1.This value does not change from object to object but rather changes on a class as a whole

2.Static properties and methods don't require an instance.

public class Foo

{

public Foo{ Counter++;

NonStaticCounter++;

}

public static int Counter { get; set; } public int NonStaticCounter { get; set; }

}

public class Program

{

static void Main(string[] args)

{

//Create an instance var foo1 = new Foo();

Console.WriteLine(foo1.NonStaticCounter); //this will print "1"

//Notice this next call doesn't access the instance but calls by the class name.

Console.WriteLine(Foo.Counter); //this will also print "1"

//Create a second instance var foo2 = new Foo();

Console.WriteLine(foo2.NonStaticCounter); //this will print "1"

Console.WriteLine(Foo.Counter); //this will now print "2"

//The static property incremented on both instances and can persist for the whole class

}

}

GoalKicker.com – C# Notes for Professionals

176