C # data type

Source: Internet
Author: User
Data | data type


Types can be divided into: value types, reference types
Value type: Store real data directly, value types have fixed lengths, and value-type variables are stored in
Stacks (Stack). As a variable of value type, each has its own data, so operations on one variable do not affect other variables.

Reference type: A reference to the memory address where the read data is stored, and a variable that is on the controlled heap (Heap) as a reference type can be cited
With the same object, so the action on one variable affects the same object that the other variable refers to.

Value types can be grouped into 3 categories:
      basic:
      integer
       int
        such as: int i=123;//can also be written as: System.Int32 i=123;
      long
         such as: Long mylong=123; / The/int type is implicitly converted to long
     Char
          such as: Char mychar= ' A '
     sbyte  byte   short   ushort UINT ULONG
   Note: (1) When writing a constant (such as: 23), the default is integral type, to represent a non integral type, you can use explicit conversion:
   such as: sbyte k=1;
       int i=2;
       k= (sbyte) i; //Explicit conversions
       i=k;         //Implicit conversion
      (2) Different integer conversion principles: A small range type can be implicitly converted to a large range type, and a wide-range type conversion
    to a small range type uses an explicit conversion.

Floating-point type
Float type: 32-bit floating-point number with a value range of
Double: 64-bit floating-point number with a value range of

Boolean type bool
True False


Structural type
struct
such as public struct point
{
int x;
int y;
string S;
}
Differences in structure and class:
A struct is a value type (on the stack), and a class is a reference
Type (on the heap).

enumerated type

Enum type
Common forms are:
Enum days {Sun,mon,tue,wed,thu,fri, Sat};
The first member value defaults to 0, the second is 1, ...
Enum days {Mon=1,tue,wed,thu,fri, Sat, Sun};
The first member value starts at 1
Enum days {mon=1,wed=3,sun,thu=8};
Examples of use of the enum type:
Class Test
{
Enum days {Sun,mon,tue,wed,thu,fri,sat};
static void Main ()
{
Console.WriteLine (days. Wed);
Days Myday=days. Fri;
Console.WriteLine (Myday);
Console.read ();
}
}

Reference type

Reference types include: classes, interfaces, arrays, delegates, and so on.

String type

String myString = "some text";
Char chfirst = mystring[2]; Result is M
When you compare two strings for equality, you can use the "= =" comparison operator directly.
You can use string when comparing two string sizes. Compare method, which returns
There are -1,0,1 three possible return values.
Examples of commonly used formats:
(1) int i=12345;
This.textbox1.text=i.tostring ();
(6) DateTime dt =new datetime (2003,5,25);
This.textbox1.text=dt. ToString ("yy"). M.D ");
Results 03.5.25
This.textbox2.text=dt. ToString ("yyyy years M");
Results May 2003
(7) int i=123;
Double j=123.45;
String s=string. Format ("i:{0,-7},j:{1,7}", i,j);
-7 represents left-aligned, 7-bit
This.textbox1.text=s;
Results i:123, j:123.45

Result 12345 (this refers to the current object, or an instance of the current class)
This.textbox2.text=i.tostring ("D8");
Result 00012345
(2) int i=123;
Double j=123.45;
String s1=string. Format ("The value is {0,7:d}", i);
String s2=string. Format ("The value is {0,7:f3}", j);
THIS.TEXTBOX1.TEXT=S1;
Results The value is 123
THIS.TEXTBOX2.TEXT=S2;
Results The value is 123.450
(3) Double i=12345.6789
This.textbox1.text=i.tostring ("F2");/result 12345.68
(4) Double i=12345.6789;
This.textbox1.text=i.tostring ("n"); Results 12,345.68
This.textbox2.text=i.tostring ("N4"); Results 12,345.6789
(5) Double i=0.126;
String s=string. Format ("The value is {0:p}", i);
This.textbox1.text=i.tostring ("P"); Result 12.6%
This.textbox2.text=s; Results The value is 12.6%


          Array
       Feature: All element types are the same.
     //one-dimensional array
     
            string[]  arr={"C", C + +};
           string[]   mystrings=new string[3];
           int[] intarr1=new int[4]{0,1,2,3};
            int[] intarr2={5,6,7};
             int nvar=5;
//Two-D array
int[,] arr = {{0,1}, {2,3}, {4,5}};
int[,] Myarr = new int[5,3];
//jagged array
string[][] stra=new string[3][];
Stra[0]=new string[2]{"A11", "A12"};
stra[1]=new string[3]{"B11", "B12", "B13"};
Stra[2]=new string[5]{"A", "E", "I", "O", "U"};


Conversions between types
The number of bits is converted to many bits, and implicit conversions can be used.
The number of bits is converted to bits with fewer digits, and explicit conversions are used.

Box and unboxing
int i=123; Numeric type
Object box=i; Boxing operations
int j= (int) box; Unboxing operation




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.