C # Language Basics FAQ Summary

Source: Internet
Author: User
Keywords nbsp; that can however be so

Overview

1. What is C #?

C # is a programming language designed by Microsoft. It is loosely based on C + +, and there are many aspects similar to Java.

Microsoft describes C # in this way: "C # is a simple, modern, object-oriented, and type-safe programming language derived from C and C + +." C # (read ' Csharp ') has been ported mainly from a family of + + + + programming languages, and the programmers of both C. and C + + are immediately familiar with it. C # Attempts to combine the rapid development capabilities of Visual Basic with the powerful and flexible capabilities of C + +. ”

2. How do I develop C # applications?

The. NET SDK includes the C # command line compiler (csc.exe), and the next version of Visual Studio (Visual Studio 7 or Visual Studio.NET) includes full support for C # development.

3, C # can replace Java?

C # is very much like the Java language-the core of both languages has similar advantages and disadvantages compared to C + +. For example, two languages have garbage collection, but two languages do not have templates (template). Microsoft has aborted the Visual J + + product, so it is difficult to assume that Microsoft is using C # instead of Java.

4. C # can replace C + +?

Obviously not, but it's hard to say C + + is new. NET platform is the best choice for writing code on the In order to make. NET is fully functional, it requires the programming language to follow certain rules-one of which is that all language types must adhere to the common type system (Common type system,cts). Unfortunately, many C + + features cannot be supported by the CTS. For example, multiple inheritance of templates and classes.

Microsoft's answer to this question is to provide Managed Extensions to C + + (Consolidator extensions,me), which enables C + + to comply with the CTS. Tag a C + + class with a CTS property (for example,. GC represents garbage collection) by adding new keywords. But it's hard to say why me C + + is more appropriate than C # when creating new projects. They are similar to features (feature), but C # is different from C + + from the outset. NET is designed for the environment. ME C + + exists for a reason that is like porting C + + code (port) to. NET environment.

So the answer to this question is probably C + + as a. NET environment will still retain its importance, and through me porting existing C + + code to fit. NET environment, but it is likely that C # will be developed by C + + developers. NET application.

8. C # is object-oriented?

Yes, C # is an object-oriented language, like Java and C + +.

9, C # has its own class library?

No, just like all of them. NET language (vb.net,jscript. NET ...) ), like C # access. NET class library, C # does not have its own class library.

Basic type

1. What standard type does C # provide?

C # supports basic types similar to C + +, including int, long, float, double, char, string, arrays, structs, and classes. However, do not assume too much, the name may be very similar, but some details are not the same. For example, long in C # is 64-bit, while C + + long depends on the platform, the 32-bit platform is 32-bit, and the 64-bit platform is 64-bit. Class and struct are almost identical in C + +, but not in C #.

2. Are all C # types derived from a common base class?

Yes, no, all objects can be considered derived from object (System.Object). But to treat a value type instance like Int,float as deriving from an object, the instance must be converted to a reference type through a boxed operation (boxing). In theory, developers can ignore these low-level transformations, but it is important to recognize that this affects system performance.

3. Can you assume that an instance of a value type can be passed as an argument to a method with an object as a parameter?

Yes, for example:

class CApplication


{


public static void Main ()


  {


int x = 25;


string s = "Fred";


Displayme (x);


Displayme (s);


   }


static void Displayme (object o)


    {


System.Console.WriteLine ("You are {0}", O);


    }


}

will display:

You are 25
You are Fred

4, what is the basic difference between a value type and a reference type?

C # divides types into two classes, one is a value type, the other is a reference type. Most intrinsic basic types (such as int, char) are value types, and structs are also value types. Reference types include classes, interfaces, arrays, and strings. The basic concept is very simple, that is, an instance of a value type represents the actual data (existing in the stack), and an instance of a reference type represents a pointer or reference to the data (in the heap).

The most confusing thing for C + + developers is that C # has already predefined some types as value types, some as reference types, and a C + + developer wants to be able to control them.

For example, in C + +, we can do this:

int x1 = 3; X1 is the value on the stack
int *x2 = new int (3)//X2 is a reference to a value of the heap

But there is no such control in C #:

int x1 = 3; X1 is the value on the stack
int x2 = new int ();
x2 = 3; X2 or the value on the stack!

5. Since int is a value type and class is a reference type, how does int derive from object?

So, when int is used as int, this is a value type (on the stack), however, when it is used as object, this is a reference type that refers to an integer value on the heap. In other words, when you think of an int as an object, the runtime automatically converts it into an object reference, which is called boxing (boxing). This conversion involves copying the values from the stack into the heap and creating a new instance of the object to reference the value. The unboxing operation (unboxing) is a reverse process--converts an object to a stack based value type.

int x = 3;
new int type on stack, value 3
Object OBJX = x;
Heap new int, set value to 3,x=3 still on stack
int y = (int) objx;
The value of the new int type is 3 on the stack, x=3 on the stack, objx=3 on the heap

6. C # uses references instead of pointers, so is C # reference the same as C + +?

Not completely, the basic idea is the same, but one important difference is that C # references can be null. So you can't confirm that C # 's reference must be a valid object. If you attempt to use a reference with a null value, a NullReferenceException exception is thrown.

For example, take a look at the following methods:

void Displaystringlength (string s)


{


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


}





If you call it this way, this method will produce a NullReferenceException exception:





string s = null;


Displaystringlength (s);





Of course there are situations in which you think that producing such an anomaly is a perfectly acceptable result,


But in this case it's best to rewrite the following code:





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 # also use them?

In C + +, a struct and a class are almost the same thing. The only difference is that the default member's access level is not the same (the default level for the struct is private for the Public,class). However, struct and class are completely different in C #. In C #, struct is a value type, and class is a reference type. In addition, struct cannot inherit from other struct or class, although struct can implement interfaces. struct does not have a destructor.

2. C # supports multiple inheritance?

C # supports multiple inheritance of interfaces, but does not support multiple inheritance of classes.

3, C # interface and C + + abstract class?

No, not exactly. Abstract classes of C + + cannot be instantiated. But it can (and often is) contain execution code and data members. A C # interface cannot contain any execution code or data members, it is just a set of method names and signatures (signature). A C # interface is more like a COM interface than an abstract class.

Another major difference is that a C # class can inherit from only one class, regardless of whether it is abstract, but can implement multiple interfaces.

4. Are C # Constructors and C + + constructors the same?

Very similar, but they are absolutely different. First, the C # destructor is not guaranteed to be invoked at a particular time. In fact it is not guaranteed to be invoked at all. The real situation is that the C # destructor is just a fake finalize method. Specifically, it is a finalize method that inserts the call to the base-class Finalize method. So this code:

class CTest


{


~ctest ()


   {


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


  }


}


is actually:


class CTest


{


protected override void Finalize ()


  {


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


base. Finalize ();


  }


}


If you don't believe it, you can add a Finalize method and a destructor to a C # class and then you can see how it is compiled.

5. What is a static constructor?

It is a constructor for the entire class, not a constructor for an instance of the class, which is invoked when the class is loaded.

6, C # All methods are virtual methods?

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

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

Using the abstract modifier before the method, the class can also be marked as abstract (this is natural). Note that the abstract method cannot have execution code (unlike pure virtual methods in C + +).

Unlike C + + processing

1. I "new" an object, but how can I delete it?

You cannot, you are not allowed to explicitly invoke a destructor, and there is no delete operator. But don't worry, garbage collection (garbage collection) will release your object, eventually (perhaps).

2. I tried to build an object on the stack, but the C # compiler doesn't pass, what's going on?

Unlike C + +, you cannot create an instance of an object on the stack. Instances of a class are always built on the heap and accept the administration of the garbage collector (garbage collection).

3. I have defined a destructor, but it never can be invoked, why?

A C # destructor is actually an implementation of the Finalize method, but the running environment does not guarantee that the Finalize method is invoked. You might consider trying it by calling the Gc.requestfinalizeonshutdown () method.

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

No, WCHAR is the same in C # in Char and C + +. All the characters in C # include strings that are Unicode, and integer values in C # are fixed-size, and in C + + the size depends on the processor. For example, a C # int is 32-bit, whereas in C + + the int is 32-bit on the 32-bit processor, 64-bit on the 64-bit processor, and a C # 's long is 64-bit.

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.