INSIDE C # 2nd Edition side

xiaoxiao2021-03-05  25

First chapter miscellaneous

1.working with assembly and modules

I didn't think that the first chapter is so depressed, all English, understanding is a bit difficult, first skip it first.

Chapter II

1.boxing and unboxing

The use of the reference is big, take a look at this code, know how many boxing and unboxin have occurred in it?

Using system;

Class test

{

Public static void main ()

{

INT i = 42; // simple unboxed value type

Object o = i; // Will Cause A Boxing Operation of i

Console.WriteLine (i "," (int32) o);

}

}

A total of 3 packages occurred

Object o = i; here is one

In i "," (int32) o First, you need to use String :: Concat to connect several parameters into a string, in the overload function of Concat,

The call parameter is Object's overload function, I to Object, one occurs, then "," Push comes in, o is converted to INT32, need

First unboxing, then boxing, and finally connected to a string.

2. Type conversion

Simple, the conversion between the classes, the inheritance class can replace the base class, and the base class cannot replace the inheritance, because the interface of the inheritance

No base category is not implemented, but the conversion can be used, but the conversion will not report in the compilation phase, but will throw at the running moment.

System.invalidcastException, therefore there is an AS, it is really a good stuff, don't worry, then throw an exception,

Will automatic assignment that cannot be converted to null, look at the code.

Class Employee {}

Class ContractemPloyee: EMPLOYEE {}

Class Castexample1

{

Public static void main ()

{

Employee E = New ContractemPloyee (); // Replacing a base class with inheritance class, compiling

ContractemPloyee CE = New Employee (); // Base class replaces inheritance class, compiling failed

ContractemPloyee CE = (ContractemPloyee) New Employee (); // Compiling but run error

ContractemPloyee c = e as contractemployee; // Effects is the same, but there is no abnormality, C is NULL

}

}

3. It is a bit different from C .

If ClassName is a name of a class

ClassName Test1; in C is a space in the stack, but C # is just declared, there is no opening space.

ClassName Test2 = new classname (); in C , it opened up a space in the heap, and no matter where C #,

Use New.

4. constant variables and read-only variables

Const and readonly, Const have already seen, readonly see

Const default is STITIC, no need to instantiate a class can be accessed, readonly needs to instantiate a class, see the example below

Using system;

Using system.net;

Class workstation {

Public Workstation ()

{

Ipaddress ipaddress =

DNS.Resolve (Hostname) .addresslist [0];

Ipaddressstring = ipaddress.tostring ();

}

Public const string hostname = "cosette";

Public Readonly String ipaddressstring;

}

Class GetIpaddress

{

Public static void main ()

{

Workstation Workstation = New Workstation ();

Console.Writeline ("The IP Address for '{0}' IS {1}",

Workstation.hostname, workstation.ipaddressstring;

}

} Using WorkStation can access iPadDressString, and HostName can use WorkStation access. If a static ReadOnly object is required, it is simple, and a Static in front of Readonly is a Static's difference between REDONLY and CONST lies in assigning the value, and const is assigned at compile time, while readonly cannot change after assigning values ​​in runtime. 5. SEALED keyword Don't want your class to inherit the other class, then add Sealed Class Point in front of the class.

{

Public Point (int x, int y)

{

X = x;

Y = Y;

}

Public int x;

Public int y;

}

6. Struct and Class Differences Struct You can access Struct RGB without NEW

{

Public int RED;

Public int Green;

Public int blue;

} RGB RGB;

Rgb.Red = 0xff;

Rgb.green = 0xff;

Rgb.blue = 0xff; but unfounded Struct can't access RGB RGB; // Error: Use of unassigned field

Console.writeline (RGB.RED); The following can be, due to the NEW process to assign RGB RGB = New RGB ();

Console.writeline (RGB.RED); feeling is the shrink version of the class, cannot be inherited, default inherited from the System.ValeType class, you can have your own constructor, the parameters of the constructor must be all members of this structure, see Code Struct RGB

{

// Constructor error

Public RGB ();

Public int RED;

Public int Green;

Public int blue;

} // Correct method Struct RGB

{

Public RGB (int Red, int Green, int Blue)

{

Red = red;

Green = green;

Blue = blue;

}

Public int RED;

Public int Green;

Public int blue;

}

转载请注明原文地址:https://www.9cbs.com/read-36114.html

New Post(0)