Thread: c++ to C#

  1. #1
    Registered User
    Join Date
    Jul 2007

    c++ to C#

    I note that in C# I can do this:
    class A {
         private StringBuilder s = new StringBuilder();
    I mean: in c++ the instance has to create in the costructor; C# permit to create in during declare? If yes, what's better?


  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    What's better? Well, I think it's largely a matter of taste. It has advantages and disadvantages.

    * By limiting initialization to the constructor, you always have all initializations in one place. That's a good thing.
    * By allowing inline initialization, you reduce the number of initializations in the constructor. If you have multiple constructors, not only do you save multiple initializations, you also don't need to ensure that they stay identical.
    * In C++, objects embedded directly are automatically initialized with the default constructor. In C#, you have to instantiate every object explicitly. The higher number of necessary initializations in C# probably tip the balance in favour of the reduced-typing solution.

    That is, in C++ you'd have
    class A
      StringBuilder s;
    and it would have the same effect as your C# code.
    All the buzzt!

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed