About C # type members, type of operation, type conversion, type classification

First, the type members

Class member definitions have (public, private, internal, protected).

Public-- of any class members are public and unrestricted

Private-- is private and only accessible by the code within a class, if not specified, the default use this keyword

Internal-- internal access only belongs in a namespace

Protected-- which can be accessed by a class or a derived class code

Class members - fields, methods, properties

public class MyClass

    {

        public int MyInt = 2019; // Fields

        public static string MyMain()

        {

            return "This is the way";

        }

        public int MyProperty {get; set;} // Properties

}

Field - a read-only, read / write data value

Method - a type of process data or the operation state of the object implementation

Properties - it can operate the same way as the class state of the image or object data, but it looks like a way to write the same field

Second, the type of operation (implicit conversion, a display converter, packing, unpacking)

Implicit conversion - for the built-in numeric type, if the value to be stored without truncation or rounding to accommodate variable implicit conversion may be small precision data transfer accuracy may be large implicit conversion

Int Num = 100;

Long Bignum= Num;

Display conversion - display conversion, also known as cast, cast explicitly tell the compiler that you intend to convert and you know a way of data loss can occur.

Double Num=100.1;

Int a;

in int.Parse = (Number);

There is a display conversion certain risks, should the errors might throw an exception or affect the accuracy of the overall results. If the display converter without knowing a type, but will cause the program conversion fails thrown.

var a = "yi";

int b = int.Parse(a);

It is possible to try to use TryParse, here is a return bool, the conversion is successful True

var a = "yi";

int b;

int.TryParse(a, out b);

Operation of the converter type - packing, unpacking

public void Test()

        {

            // packing

            int a = 1;

            object obj = a;

            // unboxing

            object obj2 = 2;

            int b = (int)obj2;

}

First we have to understand, value types are allocated on the thread stack, from garbage collection management. The reference types allocated in the managed heap.

Packing - simply converted value type is boxed reference type, detail Yes. Allocate memory in managed memory, and then copy the values ​​to the type of memory heap, and returns the address of the new object, the process of packing.

Unpacking - Packing unpacking is not the reverse process, simply reference type is converted to a value of type unpacking, unpacking detail is to obtain a pointer to the packing section, and then copy the value of part of the unboxed to the stack.

Third, the type of conversion (Is, As)

Conversion operator --is, as

as-- is an object from one type into another type, if the conversion was successful this type of return it, otherwise return null

A a=new A();

B b = A as B; // convert normal return type B, the conversion failed return null

is-- is to determine the type of conversion to another type, if the conversion is successful return True, otherwise False.

A a=new A();

If (B is A) // Returns True False

{

}

Safe judgment may be performed first and then the conversion type is used as for type conversion. Of course, this case may result in an updated performance consumption.

Easier once verification and safe operation:

A a=new A();

B b= A as B;

if(B!=null){}

else{}

Fourth, the type classification

Everything objects, each object corresponds to a class, all class has a base class --Object-System.Object

Which then can be divided into the value type (basic data types), a reference type.

Value Type: integer, floating point, character, Boolean, structure, enumeration

Reference types: arrays, strings, classes, interfaces, delegates

Comments about the type of detail will tell in the next section.

V. extending extension (abstract class)

Here we explain in detail the lower classes and abstract classes. While the difference between the direct interface with the same.

Class - abstract class:

1, can be inherited

2, an abstract class that can not be instantiated, but to inherited general class can be instantiated

3, the abstract class contains only method declarations, no method thereof. And can only exist in the abstract class

4, sub-class inherits the abstract class must implement the abstract method, unless the subclass is also abstract class

5, may be included in the abstract abstract class and instance methods

Interface - abstract class:

the same

1, can be inherited

2, there can be no realization of the method declarations

3, can not be instantiated

Method 4, the subclass must implement its stated

different:

1, multiple inheritance can abstract class interface single inheritance

2, an example method may comprise abstract class, an interface can not contain

3, the interface supports callback, abstract classes are not supported

Since there are classes, abstract classes, interfaces, so they were in when to use it?

Like do not say it, you can use under ordinary circumstances.

Abstract classes - if the functional unit needs to be designed large, the use of the abstract class. An abstract class is mainly used for close objects.

Interface - If the design of small and concise functional blocks can then use the interface, the interface is adapted to provide generic function class do not want off

For example: There are a lot of small animals, pigs, dogs, chickens, cats between a fence. For this we have designed a program, pigs, dogs, chickens, cats are animals that, according to an abstract class - large design functional unit. We can design an abstract class animal. Then write down your own specific inherited class (like pigs, dogs, chickens, cats) each person. So we need to study their sounds, where the design of small functional modules. We can use the interface to design a cry. Then each every thing to achieve different sounds (called pig, dog barks, the rooster crows, cats called). Designed to achieve a large functional unit we choose an abstract class, designed to achieve a small and concise we chose to use the interface function block.

Guess you like

Origin www.linuxidc.com/Linux/2019-07/159425.htm