Monday, September 27, 2010

Frequently Asked Questions based on C # language summary (2)


5. Since the int is a value type, while the class is a reference type, then the int is how it derives from the Object?

Is that, when used as int int time, this is a value type (the stack), however, is Object when it is used, this is a reference to the heap of the integer value of reference type. In other words, when you int as an object, run the floor will it automatically into object references, this transformation process is called boxing (boxing). The conversion includes the value stack to the heap copy, and a new instance of the object to reference the value. Unboxing operations (unboxing) is an anti-process - the object into a stack-based value type.

int x = 3;

/ / Stack on the new int type, value of 3

object objx = x;

/ / Heap new int, set the value of 3, x = 3 is still on the stack

int y = (int) objx;

/ / New value of type int 3 on the stack, x = 3 in the stack, objx = 3 in the heap

6. C # using the alternative reference pointer, then a reference to C # and C + + reference to the same?

Incomplete, the basic idea is the same, but one important difference is that C # references can be null. So you are not sure C # will be a reference to a valid object. If you try to use a reference value is null, a NullReferenceException exception will be thrown.



For example, take a look at the following method:

void displayStringLength (string s) (

Console.WriteLine ("String is length (0)", s.Length);)

If so call it, this approach will result in a NullReferenceException exception:

string s = null;

displayStringLength (s);

Of course, in some cases you have such an exception that is perfectly acceptable results, but in this case, the best rewrite the code by clicking the following:

void displayStringLength (string s) (

if (s == null)

Console.WriteLine ("String is null");

else

Console.WriteLine ("String is length (0)", s.Length);

)

class and struct

1. struct in C + + is redundant, why C # should use them?

In C + +, a structure and a class of almost a same thing. The only difference is the default level of access of members of different (struct's default level is the public, class-level default is private). However, in C # in the struct and the class is completely different. In C # in, struct is a value type, while the class is a reference type. In addition struct can not inherit from other struct or class, although struct can implement interfaces. struct no destructors.

2. C # support multiple inheritance it?

C # support multiple interface inheritance, but does not support multiple class inheritance.

3. C # interface and C + + abstract class the same?

No, not completely. C + + abstract class can not be instantiated. But it can (and often is) that contains the implementation code and data members. A C # interface can not contain any implementation code or data members, it is only a method name and signature (signature). A C # interface is more like a COM interface instead of abstract classes.

Another major difference is: C # class can only be from a class (whether or not abstract) inherited, but can implement multiple interfaces.

4. C # Builder and C + + constructor is the same?

Very similar, but they are absolutely different. First, C # destructor does not guarantee a specific time is called. In fact, it does not guarantee being called. The truth is, C # destructors is just a disguised Finalize method. Specific point of speaking, it is an insert called the base class Finalize method Finalize method. Therefore, this code:

class CTest (

~ CTest () (

System.Console.WriteLine ("Bye bye");

)

)

Actually:

class CTest (

protected override void Finalize () (

System.Console.WriteLine ("Bye bye");

base.Finalize ();

)

)

If you do not believe can be a Finalize destructor method and a device to join C # class, then you can know how to compile the.

5. What is static constructor?

It is a whole class constructor, rather than an instance of the class constructor, it is loaded when the class is called.

6. C #, all methods are virtual method?

No, like C + + as the default when the method is not virtual, but can be changed to virtual.

7. How to C # in a pure virtual function declaration?

The methodology used in the abstract modifier, a class can be marked as abstract (which is natural). Note, abstract methods can not have the code (unlike C + +, pure virtual methods).
And C + + handling of the different

1. My "new" an object, but how do I remove it?

You can not, do not allow you to explicitly call the destructors, no delete operator. But do not worry, garbage collection (garbage collection) will release your object will eventually (probably will).

2. I tried to create an object on the stack, but the C # compiler does not through, how is this going?

And C + + different, you can not stack instance of an object. Instance of the class is always built on the heap and accept the garbage collector (garbage collection) management.

3. I define a destructor device, but it can never be called, why?

A C # destructors Finalize method is actually realized, but the operating environment does not guarantee the call Finalize method. You may consider calling GC.RequestFinalizeOnShutdown () method a try.

4. Most of the C # and C + + basic types of the basic types have the same name, they are the same?

Not, C # in the char and C + + in the wchar is the same. C #, strings are all Unicode characters, including the, C # in the integer value is a fixed size, while in C + +, its size depends on the processor. For example, a C #, int is 32 bits, while the C + +, int in 32-bit processors are 32-bit, 64-bit processor in 64-bit, a C #, is 64 bits long.






Recommended links:



AVI to Zune



"Soldier Of Blood," The Official Version Of The Game Experience And Evaluation



focus on the FAMILY 3



Wang Donglin Beijing SCHOLAR Chairman



RM To MPEG4



Wizard Personal Interest



Sybase raise money for love during the full Sichuan



Cool My Music Box, To Create Your Music Kingdom



"Dream Zhu XIAN," How the experience card



C # in the delegate and event



My favorite Anti-Spam And Anti-Spy Tools



Panda burning incense on a "second chance"



Sun rushes to the fore: All Free



Cool my music box, the new interface, mainly practical



Transport Stream converter