Hello i have problem with get nested class property value. I want
class Car
{
string name;
string model;
class Engine
{
public string engineNumber;
public Engine(string engineNumber)
{
this.engineNumber = engineNumber;
}
}
public Car(string name, string model, string engineNumber)
{
this.name = name;
this.model = model;
Engine e1 = new engine(engineNumber)
}
}
In this code I try write Console.Write(c1.e1.engineNumber);
in Main class. It is possible get engineNumber
value?
Many ways to accomplish it, here is mine :), the trick is to differentiate class declaration from class implementation.
using System;
public class Program
{
public static void Main()
{
var c = new Car("Name", "Model", "Engine");
Console.WriteLine(c.engine.engineNumber);
}
class Car
{
string name;
string model;
public Engine engine;
public Car(string name, string model, string engineNumber)
{
this.name = name;
this.model = model;
this.engine = new Engine(engineNumber);
}
}
class Engine
{
public string engineNumber;
public Engine(string engineNumber)
{
this.engineNumber = engineNumber;
}
}
}
You can see it running here: https://dotnetfiddle.net/66lGn6