How to make a default value for the struct in C#?

前端 未结 5 1429
猫巷女王i
猫巷女王i 2021-01-07 20:36

I\'m trying to make default value for my struct. For example default value for Int - 0, for DateTime - 1/1/0001 12:00:00 AM. As known we can\'t define parameterless constru

5条回答
  •  孤城傲影
    2021-01-07 21:04

    Printing out objects of the C# results with namespaces unless you override .ToString() for your objects. Can you define your struct like below and try it ?

    public struct Test
    {
        int num;
        string str;
        public override string ToString()
        {
            return "Some string representation of this struct";
        }
    }
    

    PS: default(Test) gives you a struct contains default(int) and default(string) which I mean Test.num is 0 and Test.str is null

    Hope this helps

提交回复
热议问题