How to return a value using constructor in c#?

后端 未结 6 940
温柔的废话
温柔的废话 2021-01-04 03:42

I follow a convention that I won\'t use any print statements in classes, but I have done a parameter validation in a constructor. Please tell me how to return that validatio

6条回答
  •  难免孤独
    2021-01-04 04:04

    A constructor returns the type being instantiated, and, if necessary, can throw an exception. Perhaps a better solution would be to add a static method to try creating an instance of your class:

    public static bool TryCreatingMyClass(out MyClass result, out string message)
    {
        // Set the value of result and message, return true if success, false if failure
    }
    

提交回复
热议问题