The difference in object-to-object - C # basic

I am new to object oriented programming and I have one simple question. What's the difference between:

public class Calculation { private _externalObject = new ExternalClass(); public int FirstParameter {get;set;} public int SecondParameter {get;set;} public int ThirdParameter {get;set;} public int FourthParameter { get { _externalObject.Calculate(FirstParameter, SecondParameter, ThirdParameter); } } } 

and

 public class Calculation { private _externalObject; public Calculation() { _externalObject = new ExternalClass(); } public int FirstParameter {get;set;} public int SecondParameter {get;set;} public int ThirdParameter {get;set;} public int FourthParameter { get { _externalObject.Calculate(FirstParameter, SecondParameter, ThirdParameter); } } } 

I want to learn how to write the best code.

+4
source share
2 answers

In this particular case, there is no measurable difference.

If, however, you had several constructors, you would need to initialize the field in each constructor if you did not do it directly in the field declaration.

It is more a matter of personal style than anything.


Note on the design and integration of classes - if you have such an external dependency, a good OOP will require you to use DI (Dependency Injection) instead of directly creating a value inside the class. Constructor injection is a good choice:

  private ExternalClass _externalObject; public Calculation(ExternalClass externalClass) { _externalObject = externalClass; } 

The above allows you to change the behavior without changing the actual class and makes the class more testable.

+7
source

In this case, the two classes are identical. In fact, for almost all purposes, the two styles of code that you used are the same. In general, you will find that most style guides recommend using field initializers (this is especially true for static field initializers).

There is one subtle difference, but it is very unlikely that it will ever affect you.

Whenever you initialize members of the inline class, C # generates code to perform this initialization immediately before running any code in the constructor. In particular, if your constructor calls the base class constructor. Field initializers are started before the base class constructor is called, and the code in your user-created constructor must be run later. That is, the following two classes are slightly different:

 public class B : A { // This happens *before* the base-class constructor. public ExternalObject external = new ExternalObject(); public B () : base() { } } public class C : A { public ExternalObject external; public C () : base() { // This happens *after* the base-class constructor. this.external = new ExternalObject(); } } 

Note that if you do not provide a default constructor, C # automatically provides the one that calls base() for you, which makes the class “look like” to class B , even if you do not explicitly provide B() .

In practice, the difference is hardly significant. In fact, you cannot refer to this in your field initializers, so you cannot rely on the base class that is created anyway.

+2
source

All Articles