2,854 questions
You can use interface ILocation and a private Location class
public interface ILocation
{
int X { get; }
int Y { get; }
}
public class Car
{
private Location _currentLocation;
public ILocation CurrentLocation => _currentLocation;
public void SomeCarMethod()
{
_currentLocation.X = 5; // it's ok
_currentLocation = new Location(1, 2); // not preferred
}
private class Location : ILocation
{
public int X { get; set; }
public int Y { get; set; }
public Location() : this(0, 0) { }
public Location(int x, int y)
{
X = x;
Y = y;
}
}
}
public class TempClass
{
public Car MyCar { get; set; }
public void TempMethod()
{
MyCar.CurrentLocation.X = 6; // X is not accessible here
var t = MyCar.CurrentLocation.X; // it's ok
}
}
or a Location class and a private RwLocation class
public class Location
{
public int X { get; }
public int Y { get; }
}
public class Car
{
private RwLocation _currentLocation;
public Location CurrentLocation => _currentLocation;
public void SomeCarMethod()
{
_currentLocation.X = 5; // it's ok
_currentLocation = new RwLocation(1, 2); // not preferred
}
private class RwLocation : Location
{
public int X { get; set; }
public int Y { get; set; }
public RwLocation() : this(0, 0) { }
public RwLocation(int x, int y)
{
X = x;
Y = y;
}
}
}
public class TempClass
{
public Car MyCar { get; set; }
public void TempMethod()
{
MyCar.CurrentLocation.X = 6; // X is not accessible here
var t = MyCar.CurrentLocation.X; // it's ok
}
}