In this project I want to practice C# and .Net features and learn more about that
-
properties
public int number{set;get;} ---> compiler create a private field ang getter and setter (we can check via comman "ildasm" for exe file of the project) -
Indexers
Indexer make work with lists and dictionaries easier. Instead of setItem and getItem methods we can use indexer with this syntax: pulic string this[string key]
{
get{return _dictionary[key]}
set{_dictionary[key] = value}
}
For code reuse we can use inheritance(is a relationship) and composition(has a relationship)
- Composition
For implementing composition we should use the class as a private field and initialize it in the constroctor.(For example: Logger)
Problems of inheritance:
1- Easily abused by amateur designer
2- Large hierarchies
3- Fragility
4-Tightly coupeling
- Access Modifiers: 1-Public 2-Private 3-Protected (Accessible from class and derived classes) 4-Internal (Accessible from the same assembly ) 5-Protected Internal (Protected or Internal)
-Upcasting:
There is not need to conversion
Shape shape = circle
after Upcasting both objects are refer to the same place
-
Downcasting:
Circle circle2 = (Circle)shape If we cast a object to another object that is not the parent calss we will get an error so we can use AS keyboard Car car = obj AS Car (If it is not possible to cast car is null) -
Reference types are stored in heap and Value types are stored in stack
-
Boxing
The process of converting a value type instance to an object reference
Example: object obj = 10 --> convert value type Number to Object type. -
Unboxing
Opposite of boxing
Example: Object obj = 10 -> var number = (int)obj
Using Generic list instead of ArrayList prevent performance penalty(Boxing and Unboxing)
-
Method Overriding:
Modifing the implementation of an inherited method.We can Override the virtual methods of Parent class in the child class. -
Abstract:
If we have abstract method(which does not have declaration) the class of this method must be abstract.Abstract method must implemen in override methods in child class.
Abstract classes can not be instantiated.
When we want to force other developers to implement the specific design we must use abstract method. For example we want to foce developers to derived a class from the shape to implement the specific draw function we use abstaract method draw in shape. -
Sealed class:
There is not possible to derived a class from a sealed class. Using sealed class are faster in runtime because of some optimization. Using of the sealed are not recommende. String in c# is sealed class. With extension method can inherit from string.