Chapter Two: in-depth C # data types

Source: Internet
Author: User


Official definition :

Encapsulation, also known as information hiding, refers to the use of abstract data types (custom class UserInfo) to combine the operation of data and data to form an indivisible independent entity, hiding internal details as much as possible, preserving only some external interfaces and making them externally connected.

Two kinds of embodiment:

1. Encapsulating methods and properties into a class

2. Encapsulate a private field as a common attribute

Advantages of Encapsulation:

First : code reuse ;

Second : do not care about the specific implementation ; Method

Thirdly , One of the three main characteristics of object-oriented ;

Four , with security !


int [] num={1,2}

Num: The address in memory is stored as: 0x001

Nums[0]: Access to content in the heap

Here is a reference type of memory schematic for reference (the picture below will cause some discomfort, please watch with the parents, White is the Ugly painting ):

Give an array assignment schematic (just assign a copy of the contents of the stack):


The structure does not use new, it can directly assign values to its properties .

there can be fields , Properties, and methods in the struct.

The struct is a value type and the class is a reference type

in the struct, you cannot assign an initial value to a field , but you can do so in a class .

There is no default constructor in the struct , But there is a default constructor in the class

1. If the struct is modified, then he is the struct

2. Struct is a data type, and he is a value type (enumeration is also a value type)

3. There are two special types of values: enumerations and struct bodies

4. When the number of attributes in a class is relatively small, it is defined as a struct

 Public struct dog{    }

Let's analyze the reference type and value type:

Reference type: Stack and heap both open space


2. Arrays

3. Interface

4. User-defined class

Value type: Only in stack open space



4. Structural body

5. Enumeration

All data types of fathers are object

All value types Direct father is ValueType, Grandpa is Object

constructor function:

By default, if a class is defined, the system automatically generates a method that has the same name as the class and does not return a value type, or even a void, which is called a constructor!

1. The method does not have a return value, not even void

2. Method name and class name are the same

Constructor: Initializes the variable with the

 Public classDog { Public stringName {Get;Set; }  Public intAge {Get;Set; }  PublicDog (stringNameintAge ) {             This. Name =name;  This. Age =Age ; }    }
class program    {        staticvoid Main (string[] args)        {            cat cat;  " small white ";            Cat. Say ();            Console.ReadLine ();             New Dog (" rhubarb ");        }    }

Packing and unpacking:

A box is a reference type, and a value type value is loaded into a reference type variable called a boxing

Boxing: Converting a value type (stack) to a reference type (stack heap)

Unpacking: Converting from reference type (stack heap) to value type (stack)

When should the box be boxed and when should it be removed?

Analysis: Because the packing and unpacking process to wear down the performance of the system, so in the development should try to avoid packing and unpacking.

Interview questions:

int number=3; Direct parent class of value type: Valuetype,valuetype's father is an object
Console.WriteLine (number. ToString ());

Conclusion: No Boxing

7. Value type passing and reference type passing:

The difference between value passing and reference passing: there is no ref keyword, and ref is a reference pass

1 class Program2     {3         Static voidMain (string[] args)4         {5             //cat cat;6             // "Small white";7             //Cat. Say ();8             //console.readline ();9 Ten             //Dog dog = new Dog ("Rhubarb", (); One  A  -             intNUM1 =1; -             intnum2 =2; theConsole.WriteLine ("num1="+num1+", num2="+num2); -  -             //Method Invocation -Change (refNUM1,refnum2); +              -Console.WriteLine ("num1="+ NUM1 +", num2="+num2); + console.readline (); A  at         } -  -         //method Definition -         Private Static voidChange (ref intNUM1,ref intnum2) -         { -             inttemp =NUM1; inNUM1 =num2; -num2 =temp; to         } +}

Call it

Chapter Two: in-depth C # data types

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: 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.