C # Inheritance

Source: Internet
Author: User

Inheritance: Resolving code redundancy in a class;

Main function

static void Main (string[] args)

{

Rep R = New Rep ("Zhang San");

R.msg ();

}

Console.readkey ();

Parent class

public class Person

{

private string _name;

public string Name

{

get {return _name;}

set {_name = value;}

}

Public person (string name)

{

This. name = name;

}

}

Subclass, inheriting the Name property inside the parent class

public class Rep:person

{

Public Rep (string name): base (name) {}

public void Msg ()

{

Console.WriteLine ("Hello everyone, my name is {0}.") ", this. Name);

}

}

The This in the constructor: there are several parameters that represent which constructor (the constructor that invokes the current class);

Base: Call the constructor inside the parent class.

Reference namespace: using+ class name; (Calling the class inside the namespace project)

Value types and reference types:

Reference type: string, custom class, Array (three);

Others are value types: int,double,bool,char,enum,struct,decimal (decimal type);

Storage: Values of value types are stored in the stack of memory;

Stack: Allocated memory space during compilation;

The value of a reference type is stored in the heap of memory;

Heap: Dynamically allocated memory space during program run;

Difference:

1. Value types and reference types are not stored in the same place in memory;

2. When passing a value type and passing a reference type, the delivery method is different;

Value types are called value passes; reference types are referred to as reference passes;

The three functions of new:

1. Open up space in memory;

2. Creation of objects in open space;

3. Call the constructor of the object;

Equals: for string type; case-insensitive comparison;

Trim (): Go space before and after

C # Inheritance

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.