Drill down into C # data types

Source: Internet
Author: User

Value types and reference types
Common types of data
Plastic Int
Floating point Type Foalt
Double-precision floating-point type Double
String String
Boolean bool
Enumeration Enum
Value type

Value types inherit from the System.ValueType class, and each value-type object has a separate memory region to hold its own value, and the area of memory where the value type data resides is called a stack (stack). As long as you modify it in your code, the value is saved within its memory area.

Reference type

Reference types inherit from the System.Object class, and in C # Reference types mainly include arrays, classes, interfaces, and so on.

Fine-score types and reference types

Value type:

Basic data type:

Plastic Int
Long Plastic Long
Floating point Type Foalt
Double-precision floating-point type Double
Character type Char
Boolean type bool

Enum type: enum

struct type: struct

Reference type:

Class:

Base class System.Object
String String
Custom Classes Class

Interface: interface

Array: Int "", string ""

Structure

Structure definition

access modifier sturct struct name {// struct }

Features of the structure:

                                  • You can have fields in your organization or you can have methods

                        1. Fields within a structure cannot be assigned an initial value when defined
                        2. Use of structures
                          1. Structures can be defined without a new direct definition of the structure's objects
                          2. The structure must be assigned an initial value after declaring the object of the structure
                          3. Demo

Public struct Student { public int. ID/; ID public int ages // Age

Public voidSayhi () {Console.WriteLine ("School Number:"+id+"Age:"+Age )} } //structure Definition
Public Static voidMain (string[] args) {student Stu; Stu.id=1234; Stu.age= -; Stu.sayhi ();} //struct Call

Value Mode parameter passing

When a value-mode parameter is passed, the parameter is a value type, and the value is unchanged after the call, which is the reference-type-value variable

Reference method parameter passing

When a reference method parameter is passed, the value is variable regardless of the parameter value type or the reference type call

Demo: Value mode pass value type parameter
 Public void addage (int age )         {            age + +;        } Static void Main (string[] args)        {            new  Stu ();             int 3 ;            Console.WriteLine (num);            Sb.addage (num);            Console.WriteLine (num);        }

Results:

Value method pass Reference type parameter
 Public classStu { Public  intAge ;  Public voidaddage (Stu Student) {Student.age++; }    }   Public Static voidMain (string[] args) {stu SB=NewStu (); Sb.age=3;            Console.WriteLine (Sb.age);            Sb.addage (SB);        Console.WriteLine (Sb.age); }

Results:

To pass a value type parameter by means of a reference
 Public void ref int Age )         {            age + +;        } Static void Main (string[] args)        {            new  Stu ();             int 3 ;            Console.WriteLine (num);            Sb.addage (ref  num);            Console.WriteLine (num);        }

Results:

To pass a reference type parameter in a reference way
 Public classStu { Public  intAge ;  Public voidAddage (refStu Student) {Student.age++; }    }   Public Static voidMain (string[] args) {stu SB=NewStu (); Sb.age=3;            Console.WriteLine (Sb.age); Sb.addage (refSB);        Console.WriteLine (Sb.age); }
Results:

Drill down into 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.