Classes

Intro

What is an object?

We learned about variables we consider data.
We learned about Methods which provide functionality
An object stores data and functionality in one variable, an now we call the data properties and functionality methods.

What is a class?

A class is the place, where we define all of that crap mentioned above. Often considered a blueprint, but please call it class.

Constructor

When creating an object, we call the constuctor of the corresponding class.

// Declaring and Initalizing an integer number
int num = 3;
// Declaring and Initalizing a MyClass object
MyClass obj01 = new MyClass();

Constructors exsist to create an instance of a class (called an object..).

class Program {
    static void Main() {
        MyClass obj01 = new MyClass(); // Calling Constructor
    }        
}
class MyClass {
    // some properties and methods
}

If we do not define any constructors by ourself within a class, it always provides the default constructor.

A default constructor is a constructor that does not require any parameters.
If we explicity declare one or more constructors, only those construcors are available.
class MyClass {
    // constructor
    public MyClass() {
        // some code
    }
}

Constructors look nearly the same like methods, but there is no return type - I hope that makes sense, because the purpose of a construtor is to create an object, so there is no need for any return-type.

Properties

Properties expose functionality of methods using the syntax of fields.
In other languages those are called get and set methods.

Example
class MyClass {
    string text;
    public string Text {
        get { return text; }
        set { text = value; }
    }
}

In the above example the property manaages the access, we use three new keywords:

  • get: Returns the property value, so a return-instruction is neccessary.
  • set: Assigns a value to the property.
  • value: Acts like an input property, when assigning a value to a property.
A property that only assigns lower letters:
class MyClass {
    string text;
    public string Text {
        get { return text; }
        set { text = value.ToLower(); }
    }
}

How to access methods and properties of an object?

We use the dot operator, like we have already done when writing Hello World, which allows accessing any members:

Console.WriteLine("Hello World"); // Accessing the method of a class
Members and Fields

Please check out this documentation article (again): Members

Acccess Levels

To control which code can be accessed from where, we use Access Modifiers.

C# uses the following access modifiers:
  • public
  • private
  • internal
  • protected internal
  • private protected

We can use access modifiers on a lot of members, but there are certain restrictions. Using accessibility levels
For now we only use:

  • public: no restrictions, access from anywhere
  • private: accessible only within the local scope which they are declared.
class MyClass{
    public int num01;
    private int num02;
    int num03; // private by default
}

Auto Properties

Auto properties are a quickway to store values using properties. In this case there is no variable needed, since an anonymous field of the corresponding data type will be created by the runtime.

Example:
class MyClass {
    public string Text { get; set; } // Auto property
    public string ReadText { get; private set; } // Auto property Read Only
}

Static Keyword

insert content