hello,
I note that in C# I can do this:
I mean: in c++ the instance has to create in the costructor; C# permit to create in during declare? If yes, what's better?Code:class A { private StringBuilder s = new StringBuilder(); }
thanks.
hello,
I note that in C# I can do this:
I mean: in c++ the instance has to create in the costructor; C# permit to create in during declare? If yes, what's better?Code:class A { private StringBuilder s = new StringBuilder(); }
thanks.
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
and it would have the same effect as your C# code.Code:class A { private: StringBuilder s; };
All the buzzt!
CornedBee
"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