C + + programmers quickly learn C #---(iii) finish

Source: Internet
Author: User
Tags abstract definition class definition constructor finally block require
c++| Program | Programmer eight. Class
For object-oriented languages, classes are plays, and the use of classes in C # is similar to C + + and is somewhat different in detail.
Let's first look at an example of a C # class:
Cases:
Class Myclass:mybasicclass//Note the base class does not require a write qualifier
{
private int A;
public int num ()
{
return 0;
}
}
public class Mybasicclass
{
}
This writes out a qualifier for a class public,c# and some qualifiers:
Internal: can only be used in the current project, and defaults to this limit when class is the default qualifier
Public: can be used in any project
Abstract (abstract): class cannot be instantiated, only inherited, can have abstract members
Sealed (Sealed): cannot inherit
Keywords can be combined with each other.
The first thing you need to know is that C # can only have one base class, it does not support multiple inheritance and does not allow the accessibility of a derived class to be higher than its base class, which means that an inner class can be inherited from a public class, and vice versa, as already mentioned in the previous article, the object class is the highest class in C #, if a class has no base class, It implicitly inherits the object class, and you can use the method provided by object in the class.
Perhaps you remember that in C + +, the concrete implementation of the method defined within the class is implemented outside the class, but it's not allowed in C #, C # stipulates that all code must be in the class, so the method code can only be formatted with inline functions in C + +, but remember that writing in C # does not mean inline , the definition of an inline function has been canceled in C #, and. NET automatically determines whether it is inline at compile time.
A different point can be found in the above example: there is no semicolon at the end of the class definition;
From now on, explain the different uses of C # from the details:
1. In C + + noun member variables, called fields in C #, fields cannot be initialized in C + +, but can be initialized directly in C #.
2. Constructor, which is similar to C + +, but does not allow initialization of the field in it, C # also adds a static constructor
public class Mybasicclass
{
Static Mybasicclass ()
{
}
}
It does not require a qualifier, it does this by assigning a static variable to a program run
3. destructor: It's completely different from the C + + concept, because there is an automatic memory collector inside C # that can reclaim unused memory, there is no need to free up memory in the destructor. There is no destructor, but a destructor is useful in a class that derives from an interface, and the concept of an interface is described later
4. Virtual functions: C # Virtual functions and C + + are syntactically different, but the meaning is the same. Virtual functions of base classes in C # use keyword virtual to use override in derived classes
5. Operator overloading: C # operator overloading and C + + principles are basically the same, but there are some differences, except that some of the operators C # already do not allow overloading. Operators that cannot be overloaded are: = =, + =,/=, *=,%=, Bitwise assignment operator (&= ...), Boolean operator (&am P;&amp ...), assignment operator (=). At the same time in C # also has a hard rule, such as overloading the operator > must be overloaded >=, overloading the = =, you must overload!= instead resolutely, of course, there are < <= and so on

Nine. Abnormal
C # and C + + exceptions are basically the same:
Exception statement for C #:
Try
{
}
Catch
{
}
Finally
{
}
The main point of their differences is that, whether or not an exception is thrown, the last finally block must be executed immediately.

10. Unsafe code
What is unsafe code, as I have said before, using pointers is unsafe code that can be used in C #, and it is the same format as C + +, but must be placed in unsafe code snippets.
Reputation unsafe code snippets, using keyword unsafe.
Cases
public unsafe int num ()
{
return 0;
}
Unsafe code can be a method, a class, a struct
11. Interface
In C + + there is no interface in this concept. It is COM evolved. A C # interface is just a definition of functions and properties. It's like an abstract class. Its definitions and classes use similar syntax. Using keyword interface
The implementation of a class to an interface is implemented by derivation, and for an interface, a class can inherit multiple.

Postscript.
I'm going to stop the pen when I get here. Perhaps you will find that after the content of the class become simple, not detailed, indeed this is the beginning of my original intention of some deviation, but this is also a reason. First of all, I found out that in the third article, the appendix of the <<c# Advanced Programming >> book has c+ + Programmer Transition C # content, and more detailed, we can refer to this book, I just put in the grammar that you often use to explain, for some not commonly used, you can through a special C # Books for detailed understanding. Second, the content after the class has a lot to say about what C + + doesn't have, The explanation of these contents can not be compared with C + +, so there is no shortcut, can only see the C # of professional books to learn, such as interfaces, delegates, and even some of the use of classes, as well as I did not mention the attributes, events. Learn from this article in the C # Ignorant of the situation quickly through the learned knowledge of its understanding and mastery, but a language is profound, so learning C # on this basis also need to look at the classic tutorial, there is a fully understood in-depth understanding, and because C # is built in the. NET environment, so yes. NET's mechanism also needs to have certain understanding, finally I hoped that has the interest to the C # friend all to be able to grasp its essence perfectly, in the. NET epoch-making process to go out the glorious step.

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.