C# for Beginner session 21 to 30

session 21 Inheritance in c#

Why Inheritance

Pillars(支架) of Object Oriented Programming

1,Inheritance(继承)

2,Encapsulation(封装)

3,Abstraction(抽象类)

4,Poymorphism(多态)

1,Inheritance is one of the primary pillars of object oriented programming.

2,It allows code reuse.(重用)

3,Code reuse can reduce time and errors.

Note:You will specify(指定) all the common fields, properties,methods in the base class,which allows reusability.In the derived class you will only have fields, properties and methods,that are specific to them.

Inheritance Syntax

1,using : to inheritance

2,C# supports only single class inheritance.

3,C#supports multiple interface inheritance.

4,Child class is a specialization(特殊化) of base class.

5,Base classes are automatically instantiated(实例化) before derived classes.

6,Parent class constructor executes before Child Class constructor.

7,Using base keyword to specify which Constructor methods .

session 22 Method hiding in c#

using the new keyword to hide a base class memeber.You will get a compiler(编译器) warning,if you miss the new keyword.

Different ways to invoke a hidden base class member from derived class

1,Use base keyword.

2,Cast child type to parent type and invoke the hidden member

3,ParentClass PC = new ChildClass()

   PC.HiddenMethod()

session 23 Polymorphism in c#

Polymorphism is one of the primary pillars of object-oriented programming.

Polymorphism allows you to invoke derived class methods through a base class reference during runtime.

In the base class the method is declared virtual, and in the derived class we override the same method.

The virtual keyword indicates, the method can be overridden in any derived class.

session 24 Difference between method overriding and method hiding

In method overriding a base class reference variable pointing to a child class object, will invoke the overriden method in the child class

In method hiding a base class reference variable pointing to a child class object, will invoke the hidden method in the base class.

session 25 Method overloading in c#

Function overloading and method voerloading terms are used interchangeably.

Method overloading allows a class to have multiple methods with the same name, but, with a different signature(签名). So, in C# functions can be overloaded based on the number, type(int, float etc), and kind(Value, Ref or Out) of parameters.

The signature of a method consists of the name of the method and the type, kind, and the number of its formal parameters. the signature of a method does not include the return type and the params modifier. So, it is not possible to overload a function, just based on the return type or params modifier.

session 26 Why Properties

Marking the class fields public and exposing(暴露) to the external world is bad, as you will not have control over what gets assigned(赋值) and returned.

简单的说,为了保护数据安全和完整性,所以需要属性。

session 27 Properties in C#

In C# to encapsulate(封装) and protect fields we use properties

1,We use get and set accessors(访问器) to implement properties

2,A property with only get accessor is a Read only property

3,A property with only set accessor is a Write only property

4,A property with both get and set accessor is Read/Write property

Note:The advantage of properties over traditional(传统的) Get() and Set() methods is that, you can access(访问) them as if they were public fields.

Auto implemented Properties

if there is no additional logic in the property accessors, then we can make use of auto implemented properties introduced in C# 3.0

Auto-implemented properties reduce the amount of code that we have to write.

When you use auto-implemented properties, the compiler creates a private, anonymous field that can only be accessed through the property‘s get and set accessors.

session 28 Structs in C#

Structs

Just like calsses structs can have 

1,Private Fields

2,Public Properties

3,Constructors

4,Methods

Object initializer syntax(语法), introduced in C# 3.0 can be used to initialize either a struct or a class

session 29 Difference between classes and structs in c#

a struct is a value type where as a class is a reference type.

All the differences that are applicable(适用于) to value types and reference types are also applicable to classes and structs

Structs are stored(存储) on stack(栈), where as classes are stored on the heap(堆).

value type hold their value in memory where they are declared, but reference types hold a reference to an object in memory.

Value types are destroyed(销毁) immediately(马上) after the scope(应用范围) is lost, where as for reference types only the reference variable(变量) is destroyed after the scope is lost. The object is later destroyed by garbage collector.

when you copy a struct into another struct, a new copy of that struct gets created and modifications on one struct will not affect the values contained by the other struct.

when you copy a class into another class, we only get a copy of the reference variable, both the reference variables point to the same object on the heap. so, operations on one variable will affect the values contanined by the other reference variable.

session 30 Interfaces in c#

We create interfaces using interface keyword. just like classes interfaces also contains properties, methods, delegates or events, but only declarations and no implementations.

It is a compile time error to privide implementations for any interface member.

Interface members are public by default, and they don‘t allow explicit(显式) access modifiers.

Interfaces cannot contain fields.

If a class or struct inherits from an interface , it must provide implementation for all interface members. Otherwise, we get a compiler error.

A class or a struct can inherit from more than one interface at the same time, but where as, a class cannot inherit from more than oncee class at the same thime.

Interfaces can inherit from other interfaces. A class that inherits this interface must provide implementation for all interface members in the entire interface inheritance chain(一系列)

We cannot create an instance of an interface, but an interface reference variable can point to a derived class object.

C# for Beginner session 21 to 30

上一篇:【C#基础】static 关键字用法小结


下一篇:接口的三种写法