4

what is the benefit of using Nested type (instead of using variable/field-data of that class type) while creating part-of relationship (example engine is a part of Car) In other words what is the difference between following: (1)

public class Engine
{
//some attributes like engine type and constructor
 . . . 
}
public class Car
{
    Engine e;
    Public Car(string type)
      {
          e = new Engine(type);
      }
    .......
}

And

(2)

public class Car
{
   public class Engine
    {
       . . . 
    }
}

and then you can do something like

Car.Engine e;
e = new Car.Engine();
e.type = something;
Tushar
  • 49
  • 3
  • 1
    Possible duplicate of [Prefer composition over inheritance?](http://stackoverflow.com/questions/49002/prefer-composition-over-inheritance) – Viru Jan 31 '16 at 18:25
  • No particular benefit. Just a different way to reference that class by name. – usr Jan 31 '16 at 18:28
  • 3
    @Viru This isn't a duplicate of that thread. OP is asking here if nested types have any advantage or not. – Martin Dawson Jan 31 '16 at 18:31
  • Possible duplicate of [Why Would I Ever Need to Use C# Nested Classes](http://stackoverflow.com/questions/1083032/why-would-i-ever-need-to-use-c-sharp-nested-classes) – blins Jan 31 '16 at 18:33
  • Hi Viru, Bilns thank you for your reply @ viru this is not inheritance vs composition - i do understand inheritance (is-a relationship) is completely different. question is what is the difference between above 2 containment logic @ Blins - yes i did read that thread before posting the question. I only found one diff reading the post - using second approach- and defining inner class private u can completely hide its implementation from outside world (as nested type can be private) but is there anything else ? what if there is no need to declare inner class private ? – Tushar Jan 31 '16 at 19:17
  • 2
    Though it is obviously possible, I am struggling to think of a problem where I have found a pattern of defining public nested types to be a solution. For instance, if you squint your eyes when looking at your use case, you are pretty much reinventing what namespaces are usually good for. See at @Eric Lippert's answer here: http://stackoverflow.com/questions/7984529/c-sharp-public-nested-classes-or-better-option – blins Jan 31 '16 at 19:28
  • Possible duplicate of http://stackoverflow.com/questions/454218/private-inner-classes-in-c-sharp-why-arent-they-used-more-often – Ian Mercer Jan 31 '16 at 19:47

2 Answers2

1

I would say a use of nested types is for organization and specificity. The issue with a nested class like in your example is that an Engine is a necessary object to all types of vehicles, therefore it doesn't quite make sense to have it nested within a particular vehicle.

You could say that perhaps using namespaces would still be better; after all, if you put the Engine class within namespace CarsAreCool.Car, you could still refer to the object by Car.Engine, but that is really no different than an exposed property of type Engine on the Car class.

So here's where I view it as useful:

Say we have an interface IVehicle which all our vehicles implement, and the other general classes relevant to a vehicle.

public interface IVehicle 
{
    public Engine Engine;
    public int PassengerCapacity;
    public int NumberOfWheels;
}

Car, Truck, Van, Motorcycle, etc would all implement this because it's a type of vehicle. Now take the Motorcycle class for instance. Helmet is something that is pretty specific to motorcycles, and they have a few properties themselves, so it would be a bit tedious to have a property HelmetColor, HelmetSize, HelmetVisorType off Motorcycle wouldn't it? It would clearly make sense for these to be properties of the Helmet class. Thus, you nest the class, such that it is only accessible when speaking about motorcycles, and it allows you to organize the properties in a clearer manner.

public class Motorcycle : IVehicle
{
    #region IVehicle implementation 

    #endregion

    #region Nested Types

    public sealed class Helmet
    {
        public string Color;
        public string Size;
        public string VisorType;
        public bool RequiredByLaw;
    }

    public Helmet Helmet;
    public decimal Mileage;

    #endregion
}
0

Nested types are used when there are some extra functionalities that the object should have and you don't want to implement it in the object itself. It is not always necessary to use it. example: Consider you are creating a game racing car. So, the car needs to have Tires, Engine and Body. While the engine can have Turbo or not. It is not necessary to have a container for the Turbo. The turbo here is adding some extra functionality to the car's engine.

public class Car
{ 
     public Engine Engine;
     public List<Tire> Tires;
}
public class Engine
{
     public string Type;
     public string Cylinder;
     public class Turbo
     {
          public string Name;
          public string Power;
          public void CalculatePower()
          {
          }
     }
}
public class Tire
{
}
Bewar Salah
  • 567
  • 1
  • 5
  • 15