C# 传递类型对象的可选参数
C# passing an optional parameter of type object
我在这里得到了这段带有字符串参数的代码:
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("Welcome to you, {0} the {1}.", player.Name, player.Class));
String.Format()
接受一个字符串加上一个由其他字符串组成的数组 (params
),这些字符串被分配到 {0} 和 {1} 位置。
I.E
string str = String.Format("Welcome to you, {0} the {1}.", player.Name, player.Class);
DisplayText(str);
//str = "Welcome to you, bob the greatest"
否则,您将需要根据您的要求创建一个重载的 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();
时,执行重载方法会在您的智能感知中为您提供 2 个选项。
在寻找我的答案之一时,我在这里提出了我的评论。
我知道这已经得到解答,但是,您也可以使用 String Interpolation (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}.");
}
输出将是:
Welcome to you, uTeisT the Novice.
我在这里得到了这段带有字符串参数的代码:
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("Welcome to you, {0} the {1}.", player.Name, player.Class));
String.Format()
接受一个字符串加上一个由其他字符串组成的数组 (params
),这些字符串被分配到 {0} 和 {1} 位置。
I.E
string str = String.Format("Welcome to you, {0} the {1}.", player.Name, player.Class);
DisplayText(str);
//str = "Welcome to you, bob the greatest"
否则,您将需要根据您的要求创建一个重载的 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();
时,执行重载方法会在您的智能感知中为您提供 2 个选项。
在寻找我的答案之一时,我在这里提出了我的评论。 我知道这已经得到解答,但是,您也可以使用 String Interpolation (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}.");
}
输出将是:
Welcome to you, uTeisT the Novice.