C # Learning data Types-(I: Introduction)

Source: Internet
Author: User

    • C # is a strongly typed language that requires a strict type of variable, if it is an integer, it is represented by an int, if it is a string, and so on, so each variable and object  must have a declared type. It requires that all variables be defined and used, whereas the JavaScript language is a weakly typed language, which, when declaring variables, is uniformly used with the VAR keyword, regardless of the type. Do not introduce here.

  

    • There are two kinds of data types for the C # language: value types and reference types . The value type is placed in the memory stack, and the reference type is placed in the memory queue, and the content stored in memory is different, the value type holds the variable itself, and the reference type holds the reference to the variable.

    For example: The value type is equivalent to cash, it can be spent, the reference type is equivalent to a bank card, you must take the bank card to fetch the money, and then spend it, so the value type executes faster than the reference type.

Anyway

C # Value types

C # Value types have the following three kinds

    1. Simple Type (types)
    2. struct type (struct types)
    3. Enumeration type (enumeration types)

1. Simple Type

Integer, Boolean, character, floating point, decimal type

  1. Integer: There are 9 integer types in C #: Sbyte,byte,short,ushort,int,uintlong,ulong,char;
    int 8;
  2. Boolean: Boolean type has two values: True and False,
    BOOL true;          // defines a Boolean type variable and assigns a value of true  bool popstar1 = (12);     // defines a Boolean type and assigns a value to it, whose value is the result of the expression (1<2)
  3. Character type: The character type is a single Unicode character, and a Unicode character is 16 bits long.
    Char popstar='P';     // assign a value to a character variable
  4. Floating point: There are two types: float and double; they differ in the range and precision of values, and when one of the expressions has a value of float, all other types are converted to floating-point to be evaluated.
  5. Decimal type: When you define a variable and assign a value, the M suffix is used to indicate that she is a decimal type.
    decimal 1.2m;
2. Structure type

  A struct type can declare constructors, constants, fields, and so on. Although the listed function is like a class, the struct is a value type and the class is a reference type. The main idea of using the structure is to create small objects, and my understanding is that the function of the structure and the class is the same, but if the content (field, method) is too many, we use the class, because the class is a reference type, we can put this class elsewhere, when used to come in, very convenient; /c1>, if the content (field, method) can be used when the struct, because we do not need to reference, directly and the main function is placed in a block OK.

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceconsoleapplication3{structPopstar { Public stringFirstName//Last Name field         Public stringSecondname;//Name Field         PublicPopstarstringMstringN//constructor Function{FirstName=m; Secondname=N; }    }    classProgram {Static voidMain (string[] args) {Popstar P=NewPopstar"Zhang","three"); Console.WriteLine (P.firstname+p.secondname);                    Console.readkey (); }    }}
Output Result: Zhang San

3. Enumeration type

  is a unique type consisting of a specified set of constants;

(1) By default, the enumeration element is of type int, and the first element is 0, and each successive element is incremented by 1.

enum MonthNames {January,february,march,april};   // defines an enumeration that contains the January-April

(2) If you want to assign a value to the first element, you can set it to 1 as follows:

enum MonthNames {January=1,february,march,april};   // defines an enumeration that contains the January-April

(3) If you want to give each assignment can also-can make the same value, but the element name can not be the same

enum MonthNames {january=23,february=2,march=33,april=2};   // defines an enumeration that contains the January-April
class program    {        staticvoid Main (string[] args)        {                        Console.WriteLine (  int) monthnames.january);            Console.readkey ();        }    }

(4) You can also use data types that are different from int

enum monthnames:byte {january=2, february=, march=,april=};   // defines an enumeration that contains the January-April

    However, enumerations use only types that are limited to long, int, short, and byte.

C # Reference types

The reference types in C # include:

      1. Object type;
      2. class type;
      3. Interface
      4. String type;
      5. Array
1. Object type

The object type is the mother of all types, and she is the most fundamental base class of other types, so you can pay any type of value to it.

ObjectPopstarint =123;//assigning an integral type to an object variable        ObjectPopstarchar ='P';//assigning a character type to an object variable        ObjectPopstarstring ="Popstar";//Assigning a string to an object variable        ObjectPopstarbool =true;//Assigning a bool value to an object variable

2. Class type

Class type is a magical type, with attributes and methods of two elements. A property is a variable of some data type contained in this class, by the actions contained in this class.

Classes also have constructors and destructors, which are used to open up a piece of memory to hold an instance of the class, and the destructor is used to destroy the block of memory. The following is a simple user class:

 Public class User    {        string  username;                     User name         int age ;                             Name         bool  sex;                            Gender         Public string  getusername ()        {            return  username;        }    }

Simply introduce the class type here, and learn more about object-oriented.

3. Interface

Interface: is just declared but not to achieve, equivalent to an outline of an article, but no specific content. Everyone can write their own content in this outline. The interface is to standardize, unify the outline.

For a program ape, you can use the unified method name in the interface to implement the different functions of the method.

An interface can derive multiple classes, and a class can inherit from multiple interfaces, but a class can have only one base class. The interface is simple to use as follows:

1 InterfaceIface2     {3         stringgetmyname ();4     }5     classPopstar:iface6     {7         stringPopstarname ="Popstar";8          Public stringGetmyname ()9         {Ten             returnPopstarname;  One         } A     } -     class Program -     { the          -         Static voidMain (string[] args) -         { -Popstar people =Newpopstar (); + Console.WriteLine (People. Getmyname ()); -            + Console.readkey (); A}

4. String type

The string data type is, of course, used to hold strings. It inherits from object, and is sealed.

stringMyName ="Popstar";//declaring a variable of a string type and assigning it a value            stringAdd ="I"+" Love"+" You";//combine three strings into a string to assign to the add            Charp = add[0];//Remove the first word from the add string assigned to the P-character variableConsole.WriteLine (P);

5. Array type

An array is a type that can hold more than one element, but the element that holds it must be the same type, such as an array that defines a int[], and the array can only contain elements of type int.

One of the more important concepts of an array is the dimension, which defines the array in the following ways:

int[] Intarr = {1, A,2}; int[] intArr1 =New int[] {1,1,1, One,2,2};//Indefinite length            int[] intArr2 =New int[3] {1,2,3};//Fixed length            int[,] IntArr3 =New int[,] { {1,1,3},{2,2,3}};//Indefinite length            int[,] intarr4=New int[2,2]{{1,1},{2,2}};//Fixed length

C # Learning data Types-(I: Introduction)

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.