Source: Ch2_OOP.pdf · EX7.pdf · EX11.pdf · EXXXX.pdf · Bro Code C# Full Course

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?

EX7 Practice — GalleryVisitor Statistics

From EX7: static constructor message, instance constructor increments total visitors, and static report method.

What should a static ShowStatistics method read to print total visitor count?

EX7 Practice — Product Counter

Product should track total created objects with a private static counter incremented in constructor.

Where should TotalProducts++ be placed to count every created product?

EXXXX Trace — Counter Alias

Counter c3 = c1 creates an alias, not a new object.

After Counter c1 = new Counter(); Counter c2 = new Counter(); Counter c3 = c1;, what is Counter.Count?

EXXXX Trace — Global vs Instance

GlobalCount is static and shared; InstanceCount belongs to each object separately.

If c1 increments twice and c2 increments once, what are (c1.InstanceCount, Counter.GlobalCount)?

Bro Code Review — Static Access Rule

Static methods do not have this and should not directly touch instance fields.

Why can a static method not directly read instance field name?