C#传递对象类型的可选参数

C#传递对象类型的可选参数,c#,class,optional-parameters,C#,Class,Optional Parameters,我在这里得到了一个字符串参数的代码: public static void DisplayText(string Default) { foreach (char c in Default) { Console.Write(c); Thread.Sleep(25); } } 现在,我需要的是能够使这段代码工作,这样它也可以接受多个参数: DisplayText("Welcome to you, {0} the {1}.", player.

我在这里得到了一个字符串参数的代码:

public static void DisplayText(string Default)
{
    foreach (char c in Default)
    {
        Console.Write(c);
        Thread.Sleep(25);
    }
}
现在,我需要的是能够使这段代码工作,这样它也可以接受多个参数:

DisplayText("Welcome to you, {0} the {1}.", player.Name, player.Class);
但我还需要能够只将字符串参数与可为空的对象参数放在一起。我在这里尝试了以下代码:

我试着使用nullable,但毫无结果


现在,有任何指针吗?

为什么不在输入中使用
String.Format()

所谓:

DisplayText(String.Format(“欢迎您,{0}{1}.”,player.Name,player.Class))

String.Format()
接受一个字符串加上分配给{0}和{1}位置的其他字符串的数组(
params

否则,您将需要根据您的需求创建一个重载的
DisplayText()
方法

比如:

 private static void DisplayText(string message, params string[] otherStrings)
 {       
   // otherStrings will be null or contain an array of passed-in-strings 
        string str = string.Format(message, otherString);
        foreach (char c in str)
        {
            Console.Write(c);
            Thread.Sleep(25);
        }       
 }

当您键入
DisplayText()时,执行重载方法将在intellisense中为您提供两个选项每个签名一个。

为什么不在输入中使用
String.Format()

所谓:

DisplayText(String.Format(“欢迎您,{0}{1}.”,player.Name,player.Class))

String.Format()
接受一个字符串加上分配给{0}和{1}位置的其他字符串的数组(
params

否则,您将需要根据您的需求创建一个重载的
DisplayText()
方法

比如:

 private static void DisplayText(string message, params string[] otherStrings)
 {       
   // otherStrings will be null or contain an array of passed-in-strings 
        string str = string.Format(message, otherString);
        foreach (char c in str)
        {
            Console.Write(c);
            Thread.Sleep(25);
        }       
 }

当您键入
DisplayText()时,执行重载方法将在intellisense中为您提供两个选项每个签名一个。

在寻找我的答案时,我在这里提出了我的评论。 我知道这已经得到了回答,但是,您也可以使用字符串插值(C#6.0)并保持方法不变

public static void DisplayText(string Default)
{
    //I have simplified the method but you get the point
    Console.WriteLine(Default);
}

class Player
{
    public string Name { get; set; }
    public string Class { get; set; }
}

public static void Main()
{
    Player player = new Player();
    player.Name = "uTeisT";
    player.Class = "Novice";

    //Passing the parameter with new feature
    //Results in more readable code and ofc no change in current method
    DisplayText($"Welcome to you, {player.Name} the {player.Class}.");
}
输出将是:

欢迎你,新手尤特斯特


在寻找我的答案时,我在这里提出了我的评论。 我知道这已经得到了回答,但是,您也可以使用字符串插值(C#6.0)并保持方法不变

public static void DisplayText(string Default)
{
    //I have simplified the method but you get the point
    Console.WriteLine(Default);
}

class Player
{
    public string Name { get; set; }
    public string Class { get; set; }
}

public static void Main()
{
    Player player = new Player();
    player.Name = "uTeisT";
    player.Class = "Novice";

    //Passing the parameter with new feature
    //Results in more readable code and ofc no change in current method
    DisplayText($"Welcome to you, {player.Name} the {player.Class}.");
}
输出将是:

欢迎你,新手尤特斯特


看一下纪录片怎么样?可选参数:听起来您可以使用可选参数*编辑:@Crudalium You beat me:)我在阅读文档时总是迷路,但谢谢你的链接:)不要粗鲁,但即使只是谷歌搜索也可以,这是非常基本的东西@的确,我看过:)看看纪录片怎么样?可选参数:听起来您可以使用可选参数*编辑:@Crudalium You beat me:)我在阅读文档时总是迷路,但谢谢你的链接:)不要粗鲁,但即使只是谷歌搜索也可以,这是非常基本的东西@uteist确实是这样:)所以我使用了字符串格式的方法,但第二种方法看起来更吸引人,我尝试过使用它,但无法理解它到底是如何工作的。为了更好的判断(通常最好问一个新的Q),我为您添加了一个示例。所以我使用了字符串格式的方法,但第二种方法看起来更吸引人,我尝试过使用它,但未能完全理解它是如何工作的。为了更好地判断(通常最好问一个新的Q),我为您添加了一个示例。嘿,谢谢,我更喜欢它,事实上,我完全支持更干净的代码!)@SirMaxwell,至少给我一个投票吧:谢谢,我更喜欢这个,事实上,我完全支持更干净的代码!:)@麦克斯韦先生,至少给我一张赞成票吧:p