C # Data types

Source: Internet
Author: User
Tags ming

Value type

Value types originate from the System.ValueType family, and each object of value type has a separate memory region to hold its own value, and the area of memory where the value type data resides is called (Stack)

Value types primarily include basic data types (such as int,float,double)

Case:

int heightzhang=170;

int Heightli=heigtzhang;

Console.WriteLine ("Last year Zhang Hao performance:" +heightzhang+ "Li Ming's results:" +heighli);

heightli=180;

Console.WriteLine ("Last year Zhang Hao performance:" +heightzhang+ "Li Ming's results:" +heighli);

Reference type

Reference types originate from the System.Object family, and reference types in C # mainly include arrays, classes, and interfaces, etc.

Case:

Int[] Infozhang=new int[]{179,60};

Int[] Infoli=infozhang;

Console.Write ("Last year Zhang Hao's height is:" +infozhang[0]+ "weight is:" +infozhang[1]+ "Li Ming's height is:" +infoli[0]+ "weight is:" +infoli[1 ");

infoli[0]=180;

infoli[1]=70;

Console.Write ("Last year Zhang Hao's height is:" +infozhang[0]+ "weight is:" +infozhang[1]+ "Li Ming's height is:" +infoli[0]+ "weight is:" +infoli[1 ");

value types and reference types

Classification of data types

                            categories                                                                      Description

                                     basic data type                                integer: int

                                                                                                 long int:

                                                                                                 float type: float

                                                                                                  Double type: double

                                                                                                  character type: char

                                                                                                 Boolean: bool

                                              Enum type                                       enum: enum

                                              Structure type                                         structure: struct

         Reference type                                                                            base class: System.Object

                                                                                                 string: String

                                                                                                  Custom class: Class

                                             interface                                               interface: interface

Array of arrays: int[],string[]

Structure

public int id;

public int age;

public int id{

get{return ID;}

set{Id=value;}

}

public int age{

get{return age;}

set{Age=value;}


}

Definition of structure

Access modifier struct struct name {
Result body
}

The structure definition has the following characteristics:

1. There can be fields in the structure, or there can be methods

2. When defining, a field in a structure cannot be assigned a value

public struct student{
public int id;

public int age;
}

Use of structures

structure is similar to the class, in the use of structure, we should pay attention to the following aspects:

1. You can directly define the object of the structure without new.

2. After declaring an object of a struct, you must assign an initial value to the members of the struct

public struct student{

public int id;

public int age;

public void Show () {
Console.WriteLine ("Id:{0}\n Age: {1}", id,age);
}
}

static void Main (string[] args) {
Student Stu;

stu.id=1001;

stu.age=20;

Stu.show ();
}

Experience in the use of structures

A struct is a value type, and declaring a struct variable stores a new copy of the structure, that is, the system is going to open up a new storage space, so the more the structure uses, the more space it consumes.

Packing and unpacking

We say that data types can be divided into value types and reference types by storage, and they can still be converted to each other. The process of converting a value type to a reference type is called boxing, which is called unpacking

static void Main (string[] args) {
int i=123;

Object o=i;

i=456;

Console.WriteLine ("Value of value type {0}", i);

Console.WriteLine ("The value of the reference type is {0}", O);


}

Unpacking

int i=123;

Object o=i;

int j= (int) o;

Different types of parameter passing

1. Using value passing, changes to parameter values in a method cannot be persisted after the call

2. So the ref method is passed, you can keep changes to the parameter values

Value Mode parameter passing

When a value way parameter is passed, the parameter can be a reference type, or it can be a value type

Using reference types as parameters

Class voter{
public void Vote (se se) {
Se. popularity++;
}
}

Se zhang=new se ();

Zhang. age=25;

Zhang. Name= "Zhang Jing";

Zhang. Gender=gender.female;

Zhang. popularity=10;

MessageBox.Show (Zhang. Sayhi ());

Voter voter=new voter ();

Voter. Vote (Zhang);

MessageBox.Show (Zhang. Sayhi ());

Reference method parameter passing

1. Using reference types as parameters

private void Vote (ref SE SE) {
se.popularity++;
}

2. Using value types as parameters

private void Vote (ref Structse SE) {

se.popularity++;

There is no difference between the two parameter formats passed in ref mode, and the changes in the method are saved


}

C # Data types

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.