C# types can be defined within the scope of a class or struct. The enclosing type provides a kind of namespace. Access to the type is through the enclosing type with dot syntax.
class Outer
{
public interface IInner {}
public enum EInner {}
public class CInner {}
public struct SInner {}
}
var outer = new Outer();
var inner = new Outer.CInner();
You can set access levels for inner types.
Private members of the outer type are in scope for members of the inner type but not vice versa.
Some "other" developers have been working on the remote control car project remote-control-competition. You have been called in to clean up the code.
Refactor the RemoteControlCar
class to move to separate out telemetry methods and properties accessed via a Telemetry
property.
var car = new RemoteControlCar();
car.Telemetry.Calibrate();
car.Telemetry.SelfTest();
car.Telemetry.ShowSponsor("Walker Industries Inc.");
car.CurrentSponsor
// => "Walker Industries Inc."
car.Telemetry.SetSpeed(100, "cps");
car.GetSpeed()
// => "100 centimeters per second"
Ensure that the Telemetry
instance cannot be created outside the scope of the car.
Ensure that the Speed
struct cannot be used outside the scope of the RemoteControlCar
class.
Ensure that the SpeedUnits
enum cannot be used outside the scope of the RemoteControlCar
class.
Sign up to Exercism to learn and master C# with 62 concepts, 175 exercises, and real human mentoring, all for free.