4

I am beginner in object oriented programming and I have one simple question. What is 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 should I write optimal code.

2 Answers2

7

In this particular case, there isn't any measurable difference.

If, however, you had more than one constructor, you would have to initialize the field in each constructor if you didn't do it directly in the field declaration.

It is more a matter of personal style than anything.


Note on class design and integration - if you have an external dependency like that, good OOP would require you to use DI (Dependency Injection) instead of instantiating the value within the class directly. Constructor injection is a good choice:

 private ExternalClass _externalObject;

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

The above allows for modification of behaviour without changing the actual class and makes the class more testable.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
2

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

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

Whenever you initialize class members inline, C# generates code to perform that initialization just before it runs any code in the constructor. In particular, if your constructor calls into a base class constructor. Field initializers are run before the base class constructor is called, while the code in your user-supplied constructor must be run afterward. 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 don't provide a default constructor, C# automatically provides one that calls into base() for you, making your class "look like" class B even if you don't explicitly provide the B() constructor.

In practice, the difference is unlikely to matter. You can't actually reference this in your field initializers, so you can't rely on the base class being constructed in either case.

Michael Edenfield
  • 28,070
  • 4
  • 86
  • 117