vimwiki

C# classes

Classes are used to create custom types.
A classe defines the info, and methods included in a custom type The elements of a class are called member (field, method, properties…)

Declaration

To declare a class :

BasicClass myClass = new BasicClass();

Note: myClass here is technichaly a pointer

Constructor

A constructor is called each time an instance of a class is created
Like method, it can be overloaded

It initialise the fields and properties of the instance of the class. It must have the same name as the class

class Person
{  
   // field of the class
   public string name;
   
   // constructor
   public Person(string nameArgument)
   {
      // Initialize the name field with the name passed in argument when the instance is created
      name = nameArgument;
   }
}

Call it with the keyword new and the argument when creating a instance

// enter the argument of the constructor when creating a instance
Person person = new Person("Bob");

Note: In the exemple the constructor initialize the field, when it initialises a properties, it is common to use the this. keyword like so this.Properties, while not necessary, this can be useful when the property has the same name as a parameter passed to the constructor

Access modifiers

specify the accessibility of classes, methods, properties, fields, and other members of a program

Five in C#:

By default, classes are public
fields, properties, and methods are private

Field

Store a piece of data within an object
Act like a variable, each instance can have a diffrent value
Are tied with properties (next chapter)

A field can have modifier:

By default it is private

Declare a field:

- accessModifier modifier type Name; _ ```C# public class MyClass {
private int myField = 42;

public readonly string anotherField;

public static double staticField;

public virtual bool virtualField; } ```

Properties

Control how one field can be accessed or modified
It define two method, a getter and a setter:

They define the comportment of the class to get and set a field

You can create a read only or write only property by ommiting one of them

In this exemple, since the field name is private, it cannot be access outside the class. But since the property is public, the field can be modified through the property

public class Person {
    // field name
    private string name;

    // properties of the field
    public string Name {
        get { return name; }
        set { name = value; }
    }
}

Person p1 = new Person();
// set the name
p1.Name = "Daniel" 
// get the name
Console.WriteLine(p1.Name)

Auto-implemeneted property

Simpler syntax for simple properties without logic
Replace the code above

public class Person
{
  public string Name
  { get; set; }

  public string Age
  { get; set; }
}

A property can have a access modifier

public class Person
{
  public string Name
  { private get; set; }

  public string Age
  { get; private set; }
}

Static constructor

A static constructor is run once per type, not per instance
It must be parameterless
It is called automatically when a class is first used or an instance of the class is created

It is define with the static keyword followed by the name of the class
- ClassName.

public class Person {

     // properties
    public string Name { get; set; }
    public int Age { get; set; }
    


    // static constructor, is commun to all the instances of the class
    static Person() {
        static public int NumberOfLeg = 2;
    }

    // instance constructor, can differ from instance to instance
    public Person(string name, int age)
    {
        Name = name;
        Age = age;
    }
    
}


//ex:

p1.Name // "Sophie"
p2.Name // "Daniel"

p1.NumberOfLeg // 2
p2.NumberOfLeg // 2

Static class

A whole class can be static
It cannot be instantiated
Its menber are accessed by the class name

Useful to have a class that provides a set of tool
ex: a library

Math is a common static class

Math.Min(23, 97);
Console.WriteLine("Let's Go!");