简体   繁体   中英

How to pass a class as optional parameter to a method?

I have searched around here for similar problems, but couldn't find a solution for my problem. MyClass holds several data and does some type casting between different types.

How can i avoid this Error:

A value of type 'string' cannot be used as default parameter because there are no standard conversions to type Program.MyClass?

I have tried Func and declared multiple function overload to be able to pass multiple argument and handle default parameter. There should be a better way to achieve this. Hopefully you could help.

To clarify my problem i have made this code:

using System;

public class Program
{
    public class MyClass {
            public string Value { get; set; } 
            public MyClass()
            {
                Value = "";
            }
            public MyClass(string s)
            {
                Value = s;
            }
            public override string ToString()
            {
                return this.Value;
            }
    }

    // Causes CS1750
    // A value of type 'string' cannot be used as default parameter
    // because there are no standard conversions to type 'Program.MyClass'
    public static string test2(string a, MyClass b = " with default text")
    {
       return a + b;
    }

    public static string test(string a, string b = " with default text")
    {
       return a + b;
    }

    public static void Main()
    {
        Console.WriteLine(test("test1"));
        Console.WriteLine(test("test1", " with my text"));
    }
}

That's not quite possible. As the error message states, you require a standard conversion: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/conversions#standard-conversions

All we can do is to define an implicit conversion operator for the class: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/user-defined-conversion-operators

In your case, it would be something like:

public static implicit operator MyClass(string val) => new MyClass(val);

And replace your test method with something like this:

public static string test(string a, MyClass b = null)
{
    b = b ?? " with default text";
    return a + b;
}

Also note that both your test methods have the same signature if only one argument is provided, so the compiler won't know which one to use, remove the default value for the 2nd test method.

use null for default value, and replace it with default value inside method:

public static string test2(string a, MyClass b = null)
{
   if (b == null) return a + " with default text";
   return a + b.Value;
}

Add an implicit conversion operator between string and MyClass

public class MyClass 
{
        public string Value { get; set; } 
        public MyClass() : this(string.Empty) { }
        public MyClass(string s)
        {
            Value = s;
        }
        public override string ToString() => this.Value;
        public static implicit operator string(MyClass item) => item.Value;
        public static implicit operator MyClass(string value) => new MyClass(value)l;
}

You should change 2 steps (In case apply for whole class)

1. Initial default value on Constructor

public MyClass()
{
    Value = " with default text";
}

2. Change default params of the method

public static string test(string a, MyClass b)
{
   return a + b;
}

BTW, This post is helpful for you.


UPDATED: Set default value for only within function

The code above just suit in case applying for whole class. If you wanna set default value for only within function, You can try the code below

public static string test2(string a, MyClass b = null)
{
   var defaultValue = " with default text";
   return a + (b == null ? defaultValue : b.Value);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM