Source: Ch2_OOP.pdf

Static Members and Namespaces

Static fields/methods/constructors, the this keyword, and namespaces.

Static Fields

A static field belongs to the class, not to any individual instance. All objects of the class share the same value. Access it via ClassName.FieldName.

csharp
class Person
{
    public static int Instances = 0;
    public string Name;

    public Person(string name)
    {
        Name = name;
        Instances++;
    }
}

new Person("Alice");
new Person("Bob");
Console.WriteLine(Person.Instances);

Output

2

Static Fields — Exercise 1

Static fields are accessed through the class name, not through an object reference.

Fix the static field access

Hint

Static members are accessed with ClassName.MemberName, not through an instance.

How many copies of a static field exist across all instances?

Static Methods

A static method belongs to the class. It can only access static members directly — it has no this reference. Utility methods like Math.Sqrt() are static.

csharp
class MathHelper
{
    public static int Square(int n) => n * n;
    public static bool IsEven(int n) => n % 2 == 0;
}

Console.WriteLine(MathHelper.Square(4));
Console.WriteLine(MathHelper.IsEven(7));

Output

16
False

Static Methods — Exercise 1

A static method cannot reference instance members because it has no object context.

Fix the instance member access inside a static method

Hint

A static method can only access static members. Make Prefix static.

Which statement about static methods is correct?

Static Constructor

A static constructor (static ClassName()) runs once, automatically, before the first use of the class. It has no access modifier and no parameters. Use it to initialize static fields.

csharp
class Config
{
    public static string AppName;

    static Config()
    {
        AppName = "MyApp v1.0";
        Console.WriteLine("Static constructor called.");
    }
}

Console.WriteLine(Config.AppName);
Console.WriteLine(Config.AppName);

Output

Static constructor called.
MyApp v1.0
MyApp v1.0

How many times does a static constructor run?

The this Keyword

this refers to the current instance. It is necessary when a parameter name shadows an instance field with the same name.

csharp
class Point
{
    private int x;
    private int y;

    public Point(int x, int y)
    {
        this.x = x;
        this.y = y;
    }

    public void Print() => Console.WriteLine($"({x}, {y})");
}

new Point(3, 7).Print();

Output

(3, 7)

The this Keyword — Exercise 1

Without this, writing name = name inside a constructor assigns the parameter to itself, leaving the field unchanged.

Fix the shadowed field assignment

Hint

Use this.name to distinguish the instance field from the parameter name.

What does this refer to inside an instance method?

Namespaces

Namespaces organise related classes and prevent name collisions. using at the top of a file imports a namespace so you don't need the full qualified name.

csharp
using System;

namespace Geometry
{
    class Circle
    {
        public double Radius;
        public double Area() => Math.PI * Radius * Radius;
    }
}

Geometry.Circle c = new Geometry.Circle { Radius = 5 };
Console.WriteLine(Math.Round(c.Area(), 2));

Output

78.54

Namespaces — Exercise 1

Without the correct using directive, the compiler cannot find the type. Add the directive to resolve the error.

Add the missing using directive

Hint

Console lives in System. List<T> lives in System.Collections.Generic.

What is the purpose of a namespace?