Some noteable diffs between C ++ and C #

Source: Internet
Author: User
1. Can I use typedefs in C #?

No, C # has no direct equivalent of the C ++ typedef. C # does allow an alias to be specified viaUsingKeyword:

 using IntList = System.Collections.Generic.List<int>;

But the alias only applies in the file in which it is declared. A workaround in some cases is to use inheritance:

 public class IntList : List<int> { }

The pros and cons of this approach are discussed here.

2. structs are largely redundant in C ++. Why does C # Have them?

In C ++, a struct and a class are pretty much the same thing. the only difference is the default visibility level (public for structs, private for classes ). however, in C # structs and classes are very different. in C #, structs are value types (instances stored directly on the stack, or Inline within heap-based objects), whereas classes are reference types (instances stored on the heap, accessed indirectly via a reference ). also structs cannot inherit from structs or classes, though they can implement interfaces. structs cannot have Destructors. a c # struct is much more like a C struct than a C ++ struct.

3. Are C # constructors the same as C ++ Constructors?

Very similar, but there are some significant differences. First, C # supportsConstructor chaining. This means one constructor can call another:

 class Person
{
public Person( string name, int age ) { ... }
public Person( string name ) : this( name, 0 ) {}
public Person() : this( "", 0 ) {}
}

Another difference is that virtual method callwithin a constructor are routed to the most derived implementation-see can I call a virtual method from a constructor.

Error handling is also somewhat different. If an exception occurs during construction of a C # object, the destuctor (finalizer)WillStill be called. This is unlike C ++ where the Destructor is not called if construction is not completed. (thanks to Jon Jager for pointing this out .)

Finally, C # hasStaticConstructors. The static constructor for a class runs before the first instance of the class is created.

Also note that (like C ++) Some C # developers prefer the factory method pattern over constructors. see Brad Wilson's article.

 

4. Can I call a virtual method from a constructor/destructor?

Yes, but it's generally not a good idea. The mechanics of Object Construction in. NET are quite different from C ++, and this affects virtual method callin constructors.

C ++ constructs objects from base to derived, so when the base constructor is executing the object is refreshing tively a base object, and virtual method callare routed to the base class implementation. by contrast, in. net the derived constructor is executed first, which means the object is always a derived object and virtual method CILS are always routed to the derived implementation. (note that the C # compiler inserts a call to the base class constructor at the start of the derived constructor, thus preserving standard oo semantics by creating the compression sion that the base constructor is executed first .)

The same issue arises when calling Virtual Methods from C # Destructors. A virtual method call in a base destructor will be routed to the derived implementation.

5. Are C # Destructors the same as C ++ Destructors?

No. They look the same but they are very different. The C # destructor syntax (with the familiar ~ Character) is just syntactic sugar for an override of the system. object Finalize method. this Finalize method is called by the garbage collector when it determines that an object is no longer referenced, before it frees the memory associated with the object. so far this sounds like a C ++ destructor. the difference is that the garbage collector makes no guarantees aboutWhenThis procedure happens. indeed, the algorithm employed by the CLR Garbage Collector means that it may be a long time after the application has finished with the object. this lack of certainty is often termed 'non-terministic finalization', and it means that C # Destructors are not suitable for releasing scarce resources such as database connections, file handles etc.

To achieve deterministic destruction, a class must offer a method to be used for the purpose. the standard approach is for the class to implement the idisposable interface. the user of the object must call the dispose () method when it has finished with the object. C # offers the 'using' construct to make this easier.

Note that it's rarely necessary to define a destructor for a C # class-it only makes sense where the class holds direct references to unmanaged resources, which is very unusual. implementing idisposable is somewhat more commonly required, but still only necessary for a small minority of classes.

6. What types of object can I throw as exceptions?

Only instances of the system. Exception classes, or classes derived from system. exception. This is in sharp contrast with C ++ where instances of almost any type can be thrown.

 

7. How can I process command-line arguments?

Like this:

 using System;

class CApp
{
public static void Main( string[] args )
{
Console.WriteLine( "You passed the following arguments:" );
foreach( string arg in args )
Console.WriteLine( arg );
}
}

(Take A Look at Charles Cook's noptfunc project for easy command-line parsing .)

 

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.