A series of literatures on C sharp

Source: Internet
Author: User
Tags abstract arrays execution garbage collection implement include inheritance visual studio
Microsoft is planning to launch the next generation of Visual Studio 6.0--visual Studio.NET. For this package everyone is talking about the most except. NET, to count it's another sharp weapon C #. Because it is not intended to include visual j++,c# in Visual Studio.NET, it should be considered Microsoft's corresponding product for Java. So, what is C #? and C, C + + What is the difference? Here, we summarize some common problems and answers to C # to help readers who do not understand it to have a preliminary understanding of it. Because C # is more like C + +, this is the main and C + + to do a comparison.
Overview

1. What is C #?
C # is a programming language designed by Microsoft Company. 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. When is C # released?
There is no exact time, but hopefully in the second half of 2001.
3. How do I develop a C # application?
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.
4. Where can I download. NET SDK & Visual Studio 7?
You can download the Beta 2 version of the SDK in http://msdn.microsoft.com/net. If you are an MSDN universal, you can download visual Studio 7 Beta 2.
5. 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 a template (template). Microsoft has aborted the Visual J + + product, so it is difficult not to believe that Microsoft is using C # as a substitute for Java.
6. C # can replace C + +?
Obviously not, but it's hard to say that C + + is new. NET platform is the best choice to write code on. 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 of the C + + features are not 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 + + (Managed extensions,me), which enables C + + to comply with the CTS. Tag the C + + class with a CTS property (for example,.-GC represents garbage collection) by adding new keywords. But when it comes to creating new projects, it's hard to say why me C + + is more appropriate than C #. 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.
Therefore, the answer to this question is likely to be 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 is the best choice.
7. What is a simple C # program?
Can be like this:
Class CApplication {
public static void Main () {
System.Console.Write ("Hello, new. NET world!"); }}
(You cannot--c# the main () as a global function without a global function)
8. Is C # 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 are the standard types provided by C #?
The basic types supported by C # are very 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 exactly the same 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 that takes 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 most 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), while 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. OK, since int is a value type, and class is a reference type, how does int derive from object?
Not 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-the object is converted to a stack based value type.
int x = 3;
The new int type on the stack with a value of 3
Object OBJX = x;
The new int on the heap, the set value is 3,x=3 still on the 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 are C # references 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);
There are, of course, some situations where you think it's a perfectly acceptable result to produce such an exception, but in this case it's best to rewrite it in 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 is superfluous in C + +, why should C # use them?
In C + +, a struct and a class are almost exactly 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. Does C # support multiple inheritance?
C # supports multiple inheritance of interfaces, but does not support multiple inheritance of classes.
3. is the C # interface the same as C + + abstract classes?
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. is the C # constructor the same as the C + + constructor?
Very similar.
5. is C # destructor and C + + destructor the same?
No! They look the same, 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.
6. If the C # destructor is different from the C + + destructor, why use the same syntax?
It really makes us confused.
7. 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.
8. Are all the methods in C # a virtual method?
No, like C + +, the default, the method is not virtual, but can be changed to virtual.
9. How do I 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 + +).
and C + + processing is different

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. The majority of C # basic types and C + + basic types have the same name, they are the same?
No, the WCHAR in C # in Char and C + + are the same. All of 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.