C # summary of basic Language FAQs

Source: Internet
Author: User
Overview

1. What is C #?

C # is a programming language designed by Microsoft. It is loosely based on C/C ++ and has many similar aspects as 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 as 'csharp ') is mainly transplanted from the C/C ++ programming language family. C and C ++ programmers will be familiar with it immediately. C # tries to combine the rapid development capability of Visual Basic with the powerful and flexible capability of C ++ ."

2. How to develop C # applications?

The. NET sdkpackage contains the csung command line interpreter (csc.exe). The next version of Visual Studio (Visual Studio 7 or Visual Studio. NET) will contain complete support for C # development.

3. C # Can I replace Java?

C # is very similar to the Java language. The core of the two languages has similar advantages and disadvantages compared with C ++. For example, there is garbage collection in both languages, but neither of them has a template ). Microsoft has terminated the Visual J ++ product, so it is difficult to think that Microsoft is using C # To replace Java.

4. C # can replace C ++?

Obviously, no, but it is hard to say that C ++ is the best choice for writing code on the new. NET platform. To make. net runtime layer can fully play a role, it requires the programming language to follow certain specific rules-one of which is that all language types must comply with the common type System (CTS ). Unfortunately, many c ++ features cannot be supported by CTS. For example, multiple inheritance of templates and classes.

Professional 3 s station 3s8.cn

Microsoft answers this question by providing manageable extensions (me) to C ++, which enables C ++ to comply with CTS. Add a new keyword to mark the properties of the C ++ class with CTS (for example,.-GC indicates garbage collection ). However, it is difficult to explain why me C ++ is better than C # when creating a new project. In terms of features, they are very similar, but different from C ++, C # was designed in the. NET environment from the very beginning. The reason why me C ++ exists seems to be to port C ++ code to the Code in the. NET environment.

Therefore, the answer to this question is probably C ++. the language outside of the. NET environment will retain its importance, and the existing C ++ code is transplanted to fit through me.. NET environment, but C # is likely to be developed by C ++ developers. NET application.

8. C # Is it object-oriented?

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

9. C # Do I have my own class libraries?

No, just like all. NET languages (VB. NET, JScript. net. ..), C # accesses the. NET class library, and C # does not have its own class library.

Basic Type

1. C # What are the standard types?

C # supports similar basic types to C ++, including int, long, float, double, Char, String, arrays, structs, and classes. However, do not assume that there are too many names, but some details are different. For example, the long in C # is 64-bit, while the long in C ++ 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.

Professional 3 s station 3s8.cn

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

Yes, no. All objects can be viewed as derived from the object (system. object. However, to consider a value-type instance like int and float as derived from an object, this instance must be converted to a reference type through a boxing operation. Theoretically, developers can ignore these underlying conversions, but realize that this is very important to system performance.

3. Is it possible to pass a value-type instance as a parameter to a method with the 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 most basic difference between the value type and the reference type?

C # There are two types: Value Type and reference type. Most of the inherent basic types (such as int and char) are value types, while structs is also value type. The reference types include classes, interfaces, arrays, and strings. The basic concept is very simple, that is, a value type instance represents the actual data (in the stack ), an instance of the reference type indicates a pointer to data or a reference (in the heap ).

C ++ developers are most confusing: C # has predefined some types as value types and some as reference types, A c ++ developer wants to be able to control it by himself.

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 heap value.

But in C #, there is no such control:

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?

Yes. When an int is used as an int, it is a value type (on the stack). However, when it is used as an object, it is a reference type of the integer on the reference stack. In other words, when you think of int as an object, the runtime layer automatically converts it into an object reference. This conversion process is called boxing ). This conversion includes copying the value in the stack to the heap, and creating an object instance to reference the value. Unboxing is an inverse process-converting an object to a stack-based value type.

China 3 s bar 3s8.cn

Int x = 3;

// The New int type on the stack. The value is 3.

Object objx = X;

// The New int In the heap. The value is set to 3, and X = 3 is still on the stack.

Int y = (INT) objx;

// The New int value 3 is on the stack, x = 3 is on the stack, and objx = 3 is on the stack.

6. C # Use references instead of pointers. Is C # referenced like C ++?

Incomplete, the basic idea is the same, but an important difference is that the reference of C # Can be null. Therefore, you cannot confirm that the reference of C # is a valid object. If you try to use a null reference, an nullreferenceexception 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 you call it like this, this method will generate an nullreferenceexception:

String S = NULL;

Displaystringlength (s );

Of course, in some cases, you think it is acceptable to generate such an exception, but in this example, you 'd better rewrite the Code as follows:

China 3 s bar 3s8.cn

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 is redundant in C ++. Why do C # need to use them?

In C ++, a structure and a class are almost the same. The only difference is that the access level of the default members is different (the default level of struct is public, and the default level of class is private ). However, in C #, struct and class are completely different. In C #, struct is a value type, while 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 # does it support multiple inheritance?

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

3. Is the C # interface the same as the C ++ abstract class?

No, not completely. The abstract class of C ++ cannot be instantiated. But it can (and often) contain Execution Code and data members. A c # interface cannot contain any code or data member. It is only a set of method names and signatures ). A c # interface is more like a COM interface than an abstract class.

Another major difference is that C # classes can only inherit from one class (whether abstract or not), but can implement multiple interfaces.

4. C # Is the constructor the same as the C ++ constructor?

They are very similar, but they are absolutely different. First, the C # destructor cannot be called at a specific time. In fact, it cannot be called at all. In reality, the C # destructor is just a disguised Finalize method. Specifically, it is a Finalize method that inserts and calls the base class Finalize method. Therefore, this Code:

Professional 3 s station 3s8.cn

Class ctest {

~ Ctest (){

System. Console. writeline ("bye ");

}

}

Actually:

Class ctest {

Protected override void finalize (){

System. Console. writeline ("bye ");

Base. Finalize ();

}

}

If you don't believe it, you can add a Finalize method and a destructor to the C # class, and then you can know how to compile it.

5. What is a static constructor?

It is a constructor of the entire class, rather than an instance constructor of the class. It is called during class loading.

6. C # Are all methods virtual?

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

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

Use the abstract modifier before the method, and the class can also be marked as abstract (this is natural ). Note: Abstract METHODS cannot execute code (different from pure virtual methods in C ++ ).

China 3 s bar 3s8.cn

Different from C ++ Processing

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

You cannot. You cannot explicitly call the destructor, nor use the delete operator. But don't worry, the garbage collection will release your object and eventually (maybe ).

2. I tried to create an object on the stack, but the C # compiler failed. What is the problem?

Unlike C ++, you cannot create an object instance on the stack. Class instances are always built on the stack and managed by the garbage collector (garbage collection.

3. I have defined a destructor, but it can never be called. Why?

A c # destructor is actually the implementation of the Finalize method, but the runtime environment does not guarantee that the Finalize method is called. You can try it by calling the GC. requestfinalizeonshutdown () method.

4. Most C # basic types have the same names as C ++ basic types. Are they the same?

No. in C #, the char is the same as the wchar in C ++. C # All characters including strings are Unicode, And the integer value in C # is fixed, and the size in C ++ depends on the processor. For example, a C # Int Is 32-bit, while a C ++ Int Is 32-bit on a 32-bit processor and 64-bit on a 64-bit processor, a c # Long is 64-bit.

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.