Na przykład dlatego, że struktura ma zawsze bezparametrowy konstruktor i domyślne wartości. Może to doprowadzić do kwiatków.
public struct Hehe
{
}
class Program
{
static void Main(string[] args)
{
ResultType<bool> a = new ResultType<bool>();
ResultType<bool> b = new ResultType<bool>(false);
var aHasValue = a.HasValue();
Console.WriteLine(aHasValue); // true
ResultType<Hehe> omg;
Console.WriteLine(omg.HasValue()); //true
Console.WriteLine(omg.Value); // ConsoleApp10.Hehe
}
Zmienne a i b są sobie równoważne, a nie przekazałem wartości jawnie w a. Drugi przykład to przekazałem swoją inną własną strukturę. Nie musiałem nawet inicjalizować Result
a i tak metoda HasValue()
na zmiennej omg zwraca true
. Gdybym miał klasę z jednym konstruktorem to byłoby to niemożliwe. Podsumowując łatwiej wyrazić klasą brak rezultatu - nullem
.
Ty jedynie pokazałeś, że źle korzystasz ze struktur, u mnie działa normalnie. Poza tym, jak ci się udało zrobić strukturę z bezparometrowym konstruktorem?
12 class Program
1 {
2 static void Main(string[] args)
3 {
4 var a = new ResultType<string>(null);
5 var b = new ResultType<string>("");
6 var c = new ResultType<Testowy>(null);
7 var d = new ResultType<Testowy>(new Testowy());
8
9 Console.WriteLine(a.HasValue());
10 Console.WriteLine(b.HasValue());
11 Console.WriteLine(c.HasValue());
12 Console.WriteLine(d.HasValue());
13 }
14 }
15
16 public class Testowy
17 {
18 public int name { get; set; }
19 public string name2 { get; set; }
20 public string Name3 { get; set; }
21 }
22
23 public struct ResultType<T>
24 {
25 public ResultType(T val)
26 {
27 Value = val;
28 }
29
30 public T Value { get; }
31
32 public bool HasValue()
33 {
34 return Value == null;
35 }
36 }