C # for Beginner session 30

Source: Internet
Author: User

Session inheritance in C #

Why inheritance

Pillars (bracket) of Object oriented programming

1,inheritance (Inheritance)

2,encapsulation (Package)

3,abstraction (abstract Class)

4,poymorphism (polymorphic)

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

2,it allows code reuse. Reuse

3,code reuse can reduce time and errors.

Note:you would specify (designation) all the common fields, properties,methods in the base Class,which allows reusability. The derived class you'll only has fields, properties and Methods,that is specific to them.

Inheritance Syntax

1,using:to inheritance

2,c# supports only a single class inheritance.

3,c#supports multiple interface inheritance.

4,child class is a specialization (specialization) of base class.

5,base classes is automatically instantiated (instantiated) before derived classes.

6,parent class constructor executes before child class constructor.

7,using base keyword to specify which Constructor methods.

Session Method hiding in C #

Using the New keyword to hide a base class memeber. You'll get a compiler (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 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.

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

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

Session difference between method overriding and method hiding

In method overriding a base class reference variable pointing to a child class object, would invoke the Overriden method in The Child class

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

Session Method overloading in C #

Function overloading and Method voerloading terms is used interchangeably.

Method overloading allows a class to has multiple methods with the same name, but, with a different signature (signature). So, in C # functions can is 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 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 isn't possible to overload a function, just based on the return type or params modifier.

Session-Why Properties

Marking the class fields public and exposing (exposed) to the external are bad, as you'll not have control over what gets Assigned (assigned value) and returned.

Simply put, attributes are required to protect data security and integrity.

Session Properties in C #

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

1,we use Get and set accessors (accessor) to implement properties

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

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

4,a property with both get and set accessor are Read/write property

Note:the advantage of properties over traditional (traditional) Get () and Set () methods is so, you can access (access) them as if they were public fields.

Auto implemented Properties

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

auto-implemented properties reduce the amount of code, we have to write.

When do auto-implemented properties, the compiler creates a private, anonymous field that can is only accessed Throug H The property ' s get and set accessors.

Session Structs in C #

Structs

Just like calsses structs can has

1,private fields

2,public Properties

3,constructors

4,methods

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

Session difference between classes and structs in C #

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

All the differences is applicable (for) to value types and reference types is also applicable to classes and structs

Structs is stored (store) on stack (stack), where as classes is stored on the heap (heap).

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

Value types is destroyed (destroyed) immediately (immediate) after the scope (application range) is lost, where as for reference types only the Referen CE variable (variable) is destroyed after the scope is lost. The object is later destroyed by garbage collector.

When you copy a struct to another struct, a new copy of that struct gets created and modifications on one struct would no T affect the values contained by the other struct.

When you copy a class to another class, we only get a copy of the reference variable, both the reference-variables point The same object on the heap. So, operations on one variable would affect the values contanined by the other reference variable.

Session 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 implemen Tations.

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

Interface members is public by default, and they don't allow explicit (explicit) access modifiers.

Interfaces cannot contain fields.

If a class or struct inherits from the 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 M Ore than Oncee class at the same thime.

Interfaces can inherit from the other Interfaces. A class that inherits this interface must provide implementation for all interface members in the entire interface inherit Ance Chain (series)

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

C # for Beginner session 30

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.