简体   繁体   中英

Can you change the default value for a user created class c#

I'm making a game and the background is made of Tile objects. The Tile parent is abstract but I have Tile arrays to hold the children. When I make a Tile array I want it to assume that any unspecified object inside a Tile array is a BlankTile(x, y) where x and y is the position in the array.

abstract class Tile 
{
   Point position;
   public Tile(int x, int y){position = new Point(x, y);}
}

public class BlankTile : Tile
{
   public BlankTile(int x, int y) : base(x, y)
    {position = new Point(x, y);}
}
Tile[] tiles = new Tile[5];

foreach(Tile aTile in tiles)
{
   Console.WriteLine(aTile.GetType().ToString());
}
//output:
//BlankTile
//BlankTile
//BlankTile
//BlankTile
//BlankTile

(if this is impossible or the incorrect way to go about this or a loop is just simpler please let me know and please tell me about a better way)

Good question, but per C# design the default value for a class is null .

When you initialize an array of any class type, like new Tile[10] it is like assigning default(Tile) to each element. The deault keyword returns null for classes (including string ), 0 for intrinsic numeric types and enum and the default constructor for structs.

But you can create an array filled with a specific value using Enumerable.Repeat(item,count)

public abstract class Tile
{
    public static readonly BlankTile Origin = new BlankTile(0, 0);

    protected Tile(Point position)
    {
        Position = position;
    }

    public Point Position { get; }
}

public class BlankTile : Tile
{
    public BlankTile(int x, int y)
        : base(new Point(x, y))
    {  }        
}

class Program
{
    static void Main(string[] args)
    {
        Tile[] array = Enumerable.Repeat<Tile>(Tile.Origin, 100).ToArray();
    }
}

There are many ways you can achieve this:

  • You can treat null values as BlankTile s in code. This would save you some unnecessary initialization, but you need to do that everywhere, which isn't very convenient, and is also prone to errors.

  • You can create a Tiles class and change the initialization of the encompassed array in the constructor. You can provide an indexer property to emulate array access syntax if you'd like.

  • You can create a helper function Tile[] CreateTileArray(int numTiles) instead of using new [] syntax, which can also initialize the members with BlankTile s. You need to remember to call this helper function whenever you create a Tile array though.

  • Instead of multiple classes, you can use a single struct with a TileType enum, and that could be initialized to TileType.Blank as the default. Writing code handling this kind of layout usually involves many checks, so may not be as performant as separate classes with virtual methods depending on how many types of tiles you'll have.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM