That is not possible.
How you intend this property to be "arrow" by the consumer of the code, the best alternative would be to validate in the method set
of property
using System;
public class WorkingDays
{
private TimeSpan[,] _times;
public TimeSpan[,] Times
{
get { return _times; }
set
{
if (value.GetLength(0) != 7 || value.GetLength(1) != 4)
throw new Exception("Tamanho de matriz inválido");
_times = value;
}
}
}
public class Program
{
public static void Main()
{
var wd = new WorkingDays();
try
{
wd.Times = new TimeSpan[8, 4];
}
catch(Exception ex)
{
Console.WriteLine(ex.Message); //Só pra não quebrar o exemplo
}
wd.Times = new TimeSpan[7, 4];
}
}
See working on . NET Fiddle
By way of curiosity, it is possible to define arrays fixed size in contexts unsafe. However, in addition to being highly recommended, it is not possible to do this with arrays multidimensional, in classes (only for struct) and containing types outside the predefined.
An example:
void Main()
{
var wd = new UWorkingDays();
unsafe{
Console.WriteLine(wd.Times[0]);
}
}
public unsafe struct UWorkingDays
{
public fixed int Times[7];
}
Not possible. You can validate in the method
set
or you can leave the instance ready and just let the "user" handle this matrixpublic TimeSpan[,] Times { get; } => new TimeSpan[7, 4];
.– Jéf Bueno
:(, then I have no choice but to validate
set
?– Matheus Saraiva
That’s right. I was editing the comment.
– Jéf Bueno