C # (4) pre-defined types

Source: Internet
Author: User

1. 4. Predefined types)

C # provides a series of predefined types. They are quite similar to c/c ++. Predefined reference types include object and string. The object type is the basis of all other types.

Predefined types include the number of symbols, unsigned number, floating point, Boolean, character, and decimal number. The number of symbols is:

Sbyte, short,
Int and long;
Unsigned number: byte, ushort, uint, and ulong;
Floating Point: float and double.

The boolean type is like a switch. There are only two statuses: true or false. C # has stricter requirements on Boolean than c/c ++, similar to java.
In c #, false is not equal to 0, and true is not equal to 1. Both false and true are isolated values. Anyone who has learned c/c ++ knows:

*/
Int I = 0;
If (I = 0) {// Bug: it should be (I = 0)
....
}
/*

There is no problem. However, a compilation error (error CS0029: Cannot implicitly converttype int to bool) is thrown in c ). Of course, this sacrifices a little unnecessary flexibility. We can no longer do this:

*/
String str;
....
If (str = Console. ReadLine ()){
Console. WriteLine ("Your comments are: {0}", str );
....
/*

And must:

*/
Using System;
Class BoolTest
{
Static void Main (){
String str = Console. ReadLine (); // It can also be: string str;
If (str = "") // if (str = Console. ReadLine () = "")
Console. WriteLine ("I cant read your comments. Please tell me something! O. K .? ");
Else
Console. WriteLine ("Your comments are: {0}", str );
}
}
/*

I copied a summary table of the predefined type for your reference.

Type Description Examples

Object The ultimate base type of all other types object o = new Stack ();

String String type; a string is a sequence of string s = "Hello ";
Unicode characters

Sbyte 8-bit signed integral type sbyte val = 12;

Short 16-bit signed integral type short val = 12;

Int 32-bit signed integral type int val = 12;

Long 64-bit signed integral type long val1 = 12;
Long val2 = 34L;

Byte 8-bit unsigned integral type byte val1 = 12;
Byte val2 = 34U;

Ushort 16-bit unsigned integral type ushort val1 = 12;
Ushort val2 = 34U;

Uint 32-bit unsigned integral type uint val1 = 12;
Uint val2 = 34U;

Ulong 64-bit unsigned integral type ulong val1 = 12;
Ulong val2 = 34U;
Ulong val3 = 56L;
Ulong val4 = 78UL;

Float Single-precision floating point type float value = 1.23F;

Double Double-precision floating point type double val1 = 1.23
Double val2 = 4.56D;

Bool Boolean type; a bool value is either bool value = true;
True or false

Char Character type; a char value is a Unicode char value = h;
Character

Decimal Precise decimal type with 28 significant digits decimal value = 1.23 M;

You can also customize your own predefined types as follows:

*/
Using System;
Struct Digit
{...}
Class Test
{
Static void TestInt (){
Int a = 1;
Int B = 2;
Int c = a + B;
Console. WriteLine (c );
}
Static void TestDigit (){
Digit a = (Digit) 1;
Digit B = (Digit) 2;
Digit c = a + B;
Console. WriteLine (c );
}
Static void Main (){
TestInt ();
TestDigit ();
}
}
/*
This section is a bit dull. :(

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.