Example #1
0
        // Q. can you propose alternatives to the parsms idiom
        //    for setting error messages?

        //params enables methods to receive variable numbers of parameters/arguments....0 to infinity # of Parameters

        //public static EnResult<T> Failure(T data, string[] errors) // without param
        //var result = EnResult<MyNote>.Failure(note, new string[] { "this", "that", "the other thing" }); //can call the method with an array as a parameter

        public static EnResult <T> Failure(T data, params string[] errors)
        {
            var x = new EnResult <T>(data)
            {
                IsSuccess = false
            };

            x.Errors.AddRange(errors);
            return(x);
        }
Example #2
0
        static void Main(string[] args)
        {
            var note = new MyNote {
                Note = "Foobaz"
            };

            //Refactoring to replace var with an explicit type
            //EnResult<MyNote> result = EnResult<MyNote>.Failure(note, "this", "that", "the other thing");

            //Target-typed new-expressions C# 9.0 - an expression gets the type from the context it is used in
            //the new expression gets the type from the context,
            //which means you don’t have to specify the target-type explicitly to call a constructor.
            //EnResult<MyNote> result = new().Failure(note, "this", "that", "the other thing");
            //var vs target-typed new expressiong - the C# compiler produces exactly the same Intermediate Language code

            var result = EnResult <MyNote> .Failure(note, "this", "that", "the other thing");

            Console.WriteLine(result.Errors.Count());
        }