[C #] Chapter 1 learning points,

Source: Internet
Author: User

[C #] Chapter 1 learning points,

Category: C #, VS2015

Created on:

Teaching materials: (12th Five-Year Plan teaching materials) C # program design and application tutorial (version 3rd)

I. Highlights of this Chapter

C # data types are classified into two categories: Value Type and reference type.

Value Type: the data value stored in the Stack.

Reference Type: the stack stores the reference addresses of objects in the Heap.

Stacks and Heap are essentially the areas opened in the memory. Both stacks and Heap are created. the. NET Framework manages the program in a unified manner, which can prevent memory leakage and other problems caused by direct delivery of memory to messy programs written by unqualified programmers.

Ii. Value Type

Value types include simple type, enumeration type, structure type, and null type.

Value Type features: the data value is saved in the stack.

1. Simple Type

Simple data types include integer, floating point, Boolean, and complex.

(1) Integer

A total of 8 types.

Sbyte: represents a signed 8-bit binary INTEGER (1 byte). The decimal value ranges from-128 ~ 127.

Byte: represents an unsigned 8-bit binary INTEGER (1 byte), ranging from 0 ~ 255.

Short: indicates a signed 16-bit binary INTEGER (2 bytes), ranging from-32768 to decimal ~ 32767.

Ushort: Unsigned 16-bit binary INTEGER (2 bytes), ranging from 0 ~ 65535.

Int: indicates a signed 32-bit binary INTEGER (4 bytes), ranging from-2147483648 to decimal ~ 2147483648.

Uint: represents an unsigned 32-bit binary INTEGER (4 bytes), ranging from 0 ~ 4294967295, the specified type is U.

Long: indicates a signed 64-bit binary INTEGER (8 bytes), ranging from-9223372036854775808 ~ 9223372036854775808, the type specified character is L.

Ulong: indicates an unsigned 64-bit binary INTEGER (8 bytes), ranging from 0 ~ 18446744073709551615, the type specified character is UL.

For example:

Int a = 1234;

Long x = 1234L;

Long y = 1234; // The int value 1234 is implicitly converted to the long type.

Long z = 0x12ab; // declare a long integer variable x and assign it a hexadecimal value of 12ABH.

(2) floating point

There are 3 types.

Float: 32-bit (4-byte) IEEE Single-precision floating point number. The precision is 7 digits after the decimal point. The value range is 1.5*10 ^-45 ~ 3.4*10 ^ 38, type specified character: F

Double: 64-bit (8-byte) IEEE double-precision floating point number. The precision is 15 to the decimal point ~ 16 bits, value range: 5.0*10 ^-324 ~ 1.7*10 ^ 308, type specified character is E

Decimal: 128-bit (16-byte) high-precision floating-point number with a decimal precision of 28 ~ 29 BITs, value range: 1.0*10 ^-28 ~ 7.9*10 ^ 28, the type specified character is M.

For example:

Float x = 2.3F; // x = 2.3

Double y = 2.7E + 23; // y = 2.7*10 ^ 23

Decimal myMoney = 300.5 M;

Decimal y = 9999999999999999999999999 M;

Decimal x = 123.123456789 M;

(3) Boolean

There are only two possible values: true and false.

For example:

Bool isMale = false;

Bool B = (I> 0 & I <10 );

(4) balanced

Char indicates a single Unicode character. The length of a Unicode character is 1 (two bytes ).

For example:

Char c1 = 'a ';

Char c2 = '\ x0041'; // hexadecimal representation of the letter ""

Char c3 = '\ u0041'; // Unicode representation of the letter ""

2. Enumeration type

In C #, the enumerated type (enumeration for short) represents a group of named constants.

(1) Definition

When defining an enumeration type, all constants use a basic type (any of the eight integer types). If the basic type is not int, you need to use a colon to specify the basic type.

For example:

Using System;

Namespace ConsoleApplication1

{

Enum Days {Sun, Mon, Tue}; // Sun: 0, Mon: 1, Tue: 2

Enum Numbers: byte {x1 = 3, x2 = 5 };

ClassClass1

{

Days day = Days. Sun;

}

}

(2) Common Methods

Obtain the names of all symbols defined in the enumerated type:

String [] dayNames = Enum. GetNames (typeof (Days ));

Obtains the values corresponding to all Symbol names defined in the enumerated type.

Int [] dayIndex = (int []) Enum. GetValues (typeof (Days ));

3. Structure Type

The. NET Framework has predefined some structures, such as System. DateTime. You can also customize the structure with the keyword struct.

Struct is a lightweight class.

Any implementation using struct can also be implemented using class. If you don't know which one is better at first, use class first. When you do the actual project, you will gradually understand that in some cases, changing some classes to struct will significantly improve the execution efficiency.

4. Empty type

That is, it can be a null value type, which is short for the generic Nullable <T>. For example, the age field in a record in the database may be an integer or null. When you assign this field to an age variable, age is essentially a value type that can be null.

For example:

Nullable <int> x = 5;

Nullable <double> y = 3.4;

The two statements are written in the following way:

Int? X = 5;

Double? Y = 3.4;

The two methods have the same functions.

Iii. Reference Type

Including: string type (string for short), class type (class for short), interface (interface for short), array type (array for short) and delegate type (delegate ).

1. String

String represents a series of UniCode characters.

For example:

String str1 = "ABCD ";

2. Array

Commonly used are one-dimensional and two-dimensional.

One-dimensional:

Int [] a = {1, 2, 3 };

Two-dimensional:

Int [,] B =

{

{1, 2, 3 },

{4, 5, 6}

}

3. Class

The. NET Framework already provides you with tens of thousands of classes, and you can use them to write various logic Code directly. In addition, you can define your own class.

The essential meaning of a class: It is to classify things that are messy together and describe them with code. For example, apple, pear, jujube, and persimmon are all mixed together. If you want to separate them for processing, You have to classify them first.

Abstract: extract the items that are common to apple, pear, jujube, and persimmon (for example, there is water), and then describe this class with a separate name, which is abstract. Of course, if the extraction is unreasonable, the extracted information may also be unique.

We will introduce it in a specific chapter later.

4. Interfaces

The essential meaning of the interface: Party A and Party B agree that Party A is responsible for buying TV and refrigerator, and Party B is responsible for buying furniture and pots and pans. This is the interface declared between Party A and Party B. However, none of the models to buy or buy are mentioned, that is, the interface has only the declaration part, but not the implementation part.

If Party A arranges Party A to buy a TV and Party A to buy a refrigerator, Party A's specific implementation is completed by Party C and Party A, not necessarily by Party.

The. NET Framework already provides many interface definitions and implementations. You can use them to write various logic Code directly. In addition, you can also define interfaces by yourself.

We will introduce it in a specific chapter later.

5. Delegate

It is the encapsulation of pointers, which can prevent inexperienced programmers from messing up the memory.

We will introduce it in a specific chapter later.

6. Events

Events can be used to quickly respond to user operations (such as mouse, keyboard, touch, and slide ).

Events are implemented by delegation.

Iv. Conversion Between Data Types

1. Conversion between value types and value types

(1) implicit conversion

If the value range is small, it must be in a large range. For example, to convert the number of int (4 bytes) types to long (8 bytes) type storage, since it is certainly okay, the compiler will do it directly, and you don't need to tell it. For example:

Int a = 15;

Long B =;

(2) Explicit conversions

The conversion from a large range to a small range: You must explicitly tell the compiler that you want to do this without considering whether data is lost. For example:

Long B = 12345;

Int a = (int) B;

(3) implementation using the Convert () method

You can also use the. Convert (...) method in the System namespace for conversion. In some special cases, it is very convenient to use this method for conversion.

2. Conversion between the reference type and the reference type

Assume that Class B inherits from Class A, that is, B is A child of Class:

Class {...}

Class B: {...}

In addition, we assume the following code is available:

A a = new ();

B B = new B ();

You can also write (implicit conversion) as follows ):

A B = new B ();

3. Conversion between value types and reference types

In the. NET Framework, the conversion between value types and reference types is achieved by "Packing" and "unpacking.

4. Conversion between strings and values

(1) convert a string to a numeric value

Commonly used:

Int. Parse (...), int. TryParse (...)

Double. Parse (...), double. TryParse (...)

(2) convert numeric values to strings

Commonly used:

String. Format (...) or $ prefix

ToString ()

5. Operators

1. Basic Operators

Same as C ++.

2. is and as operators

The is operator is used to check whether an object is compatible with a given type. For example:

If (obj is MyObject)

{

//......

}

If the provided expression is not empty and the provided object can be forcibly converted to the provided type without causing an exception, the is expression returns true. Note that the is operator only considers reference conversion, boxing conversion, and unboxing conversion. Do not consider other conversions (such as user-defined conversions ).

The as operator is similar to a forced conversion operation. If the conversion fails, null is returned, but no exception is thrown.

3. typeof

The System. Type object used to obtain the Type. For example:

System. Type type = typeof (int );

The preceding statement can also be implemented using the following method:

Int I = 0;

System. Type type = I. GetType ();

4. Other operators

The subsequent sections of other operators will be involved.

Vi. Expressions

1. Basic operators and expressions

Same as C ++.

2. LINQ query expression

Next, we will introduce it separately.

3. Lambda expressions

Next, we will introduce it separately.

VII. Process Control statements

C # process control statements are program commands. Unless otherwise specified, the statements are executed in sequence.

1. Basic statements

C # has the following basic statements:

(1) Branch statement: if-else, switch

(2) loop statement: while, do-while, for, foreach

(3) Jump statement: break, continue, goto, return

(4) exception handling statements: throw, try-catch, try-catch-finally, try-finally

2. using statements

Provides convenient syntax to ensure correct use of the IDisposable object. For example:

Using (Font font1 = new Font ("Arial", 10.0f), font2 = new Font ("Arial", 10.0f ))

{

// Use font1 and font2

}

3. Other statements (understanding)

The following are all the statements used in special cases, which are not covered in the textbooks.

(2) yield return; yield break;

Meaning and usage see: https://msdn.microsoft.com/zh-cn/library/9k7k7cf0.aspx

(3) checked, unchecked

Meaning and usage see: https://msdn.microsoft.com/zh-cn/library/khy08726.aspx

(4) fixed

Meaning and usage see: https://msdn.microsoft.com/zh-cn/library/f58wzh21.aspx

(5) lock

Meaning and usage see: https://msdn.microsoft.com/zh-cn/library/c5kehkcz.aspx

8 * Insecure code and pointers (understanding)

To ensure type security, pointer is not supported by C # by default. However, if you want to use pointers to control the buffer, you can use the unsafe keyword to define the insecure context that can use pointers.

The teaching material does not cover this aspect. For more information, see:

Https://msdn.microsoft.com/zh-cn/library/t2yzs44b.aspx

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.