C# 我想将out参数与字符串变量一起使用
我想将out参数与字符串变量一起使用 例如:C# 我想将out参数与字符串变量一起使用,c#,.net,C#,.net,我想将out参数与字符串变量一起使用 例如: string Url = "https://docs.microsoft.com/tr-tr/dotnet/csharp/language-reference/keywords/out-parameter-modifier" string BaseUrl = this.DecompositionUrl(out Url).BaseUrl; string ServiceUrl = this.DecompositioUrl(out Url).Servic
string Url = "https://docs.microsoft.com/tr-tr/dotnet/csharp/language-reference/keywords/out-parameter-modifier"
string BaseUrl = this.DecompositionUrl(out Url).BaseUrl;
string ServiceUrl = this.DecompositioUrl(out Url).ServiceUrl;
我想用这样的方法
public static string DecompositionUrl(out string Urlr)
{
// enter here :
BaseUrl = Urlr.Substring(0,40);
ServiceUrl = Urlr.Substring(40,Urlr.Length);
}
当返回我的
DecompositionUrl
时,我想设置BaseUrl
和ServiceUrl您需要为您打算从方法返回的两个值声明out
参数:BaseUrl
和ServiceUrl
。源url
需要通过普通参数传入(而不是out
)
然后您可以这样调用您的方法:
string url = "https://docs.microsoft.com/tr-tr/dotnet/csharp/language-reference/keywords/out-parameter-modifier"
DecompositionUrl(url, out var baseUrl, our var serviceUrl);
Console.WriteLine($"Base URL is {baseUrl} and service URL is {serviceUrl}.");
C#7允许您返回一个“tuple”,作为这样一个方法的结果
public static (string, string) DecompositionUrl(string url)
{
var baseUrl = ...;
var serviceUrl = ...;
return (baseUrl, serviceUrl);
}
(string baseUrl, string serviceUrl) = DecompositionUrl("https://docs.microsoft.com/tr-tr/dotnet/csharp/language-reference/keywords/out-parameter-modifier");
你可以这样使用它
public static (string, string) DecompositionUrl(string url)
{
var baseUrl = ...;
var serviceUrl = ...;
return (baseUrl, serviceUrl);
}
(string baseUrl, string serviceUrl) = DecompositionUrl("https://docs.microsoft.com/tr-tr/dotnet/csharp/language-reference/keywords/out-parameter-modifier");
我认为这比使用out
关键字要好,因为它更清楚什么是输入,什么是输出。您应该将out
参数更改为带有BaseUrl
和ServiceUrl
属性的自定义类。