In this lesson we’ll learn how to create your own types.

Agenda

Class example

It’s been said in lesson 1 that class is a ‘description’ for objects. When you define a class, you define a blueprint for an object.

Basically class consists of:

  • Class name
  • Constructors. They are used to create and initialize any object member variables when you use the ‘new’ expression to create an object of a class.
  • Members. It’s some variable inside a class.
  • Properties. A property is a member that provides a flexible mechanism to read, write, or compute the value of a private field.
  • Methods (functions). A function allows you to encapsulate a piece of code and call it from other parts of your code.

Following example demonstrates a class with all its basic features:

class Calculator
{
    public Calculator(string userName)
    {
        Console.WriteLine($"Hello {userName}.");
    }

    //Pay attention, it's a static constructor
    static Calculator()
    {
        Console.WriteLine("I'm a calculator. Let's have some fun.");
    }

    public double Mutliply(double x1, double x2)
    {
        var result = x1*x2;
        AddHistory(x1, x2, "*", result);
        return result;
    }
    public double Sum(double x1, double x2)
    {
        var result = x1 + x2;
        AddHistory(x1, x2, "+", result);
        return result;
    }

    public double Substract(double x1, double x2)
    {
        var result = x1 - x2;
        AddHistory(x1, x2, "-", result);
        return result;
    }

    public double Divide(double x1, double x2)
    {
        var result = x2 != 0 ? x1/x2 : 0; //Preventing infinity value.
        AddHistory(x1, x2, "/", result);
        return result;
    }

    private StringBuilder _history = new StringBuilder();
    private void AddHistory(double x1, double x2, string operation, double result)
    {
        _history.AppendLine($"{x1} {operation} {x2} = {result}");
    }

    public string GetHistory()
    {
        return _history.ToString();
    }

    public void InviteToEnterValue(out double value)
    {
        Console.WriteLine($"Enter value:");
        value = double.Parse(Console.ReadLine());
    }
}

And it can be used in following way:

Calculator calculator = new Calculator("Alex");

var stopCalculation = false;
do
{
    double a;
    double b;
    double sumResult = 0;
    calculator.InviteToEnterValue(out a);
    calculator.InviteToEnterValue(out b);
    Console.WriteLine("Write a command:");
    var command = Console.ReadLine();

    switch (command)
    {
        case "+":
            sumResult = calculator.Sum(a, b);
            break;
        case "-":
            sumResult = calculator.Substract(a, b);
            break;
        case "*":
            sumResult = calculator.Mutliply(a, b);
            break;
        case "/":
            sumResult = calculator.Divide(a, b);
            break;
        default:
            stopCalculation = true;
            Console.WriteLine("Wrong command. Calcullation stopped.");
            break;
    }

    Console.WriteLine($"{a} {command} {b} = {sumResult}");

} while (!stopCalculation);

Access modifiers

  • public: access is not restricted
  • private: access is limited to the containing type
  • protected: access is limited to the containing class or types derived from the containing class
  • internal: access is limited to the current assembly
  • protected internal: access is limited to the current assembly or types derived from the containing class

Static

Static member in class belongs to the type itself, not to a specific object. Static member or function can be defined with ‘static’ modifier.

Example of static class:

class Animal
{
    //static Property
    public static int AnimalCount { get; private set; }

    private string Name;

    public Animal()
    {
        Name = "No name defined.";
        AnimalCount++;
    }

    public Animal(string name)
    {
        Name = name;
        AnimalCount++;
    }

    public void PrintName()
    {
        Console.WriteLine(Name);
    }

    //static function
    public static void GetInfo()
    {
        Console.WriteLine("My mission is to be an animal");
    }
}

And its using example:

Animal.GetInfo(); //prints message
Animal a1 = new Animal();
Animal a2 = new Animal("Cow");
Animal a3 = new Animal();
a2.PrintName();//prints Cow
a3.PrintName();// pring 'no name defined' message
Console.WriteLine(Animal.AnimalCount); // outputs 3

Static constructors

A static constructor is used to initialize any static data, or to perform a particular action that needs to be performed once only. It is called automatically before the first instance is created or any static members are referenced. Static constructor defines with out any access modifier.

Static class is demonstrated in the Calculator example above:

class Calculator
{
    /////////////

    static Calculator()
    {
        Console.WriteLine("I'm a calculator. Let's have some fun.");
    }

    /////////////
  }

C# Coding Conventions

Such guidelines are used by Microsoft. It’s kindly recommended to use it o your projects.

C# Coding Conventions

References

Access modifiers are keywords used to specify the declared accessibility of a member or a type. This section introduces the four access modifiers:

Presentation

Lesson 4 from Alex Honcharuk