C# 满足条件时将字符串数组值作为返回类型字符串传递

C# 满足条件时将字符串数组值作为返回类型字符串传递,c#,arrays,string,function,return-type,C#,Arrays,String,Function,Return Type,我有一个模块,它返回字符串数组“string[]”。它包含成功代码和作者姓名 var get_author = SetBookInfo(Id, Name); 此函数SetBookInfo返回响应代码和作者姓名。 我的情况是: 如果响应代码为“success”,则返回作者姓名“william”。[“成功”,“威廉”] 如果响应代码为“失败”,则返回“失败” 我该怎么做?请验证我的方法是否正确。还有其他方法吗?请帮忙 也许这就是你想要的: public string GetAuthorName()

我有一个模块,它返回字符串数组“string[]”。它包含成功代码和作者姓名

var get_author = SetBookInfo(Id, Name);
此函数SetBookInfo返回响应代码和作者姓名。 我的情况是:

如果响应代码为“success”,则返回作者姓名“william”。[“成功”,“威廉”]

如果响应代码为“失败”,则返回“失败”


我该怎么做?请验证我的方法是否正确。还有其他方法吗?请帮忙

也许这就是你想要的:

public string GetAuthorName()
{
    var get_author = SetBookInfo(Id, Name); // returns string[]

    if (get_author != null && get_author.Length > 0)
        {
            if(get_author[0] == "success") return get_author[1]; //e.g. ["success", "william"], "william" will be returned
            else if (get_author[0] == "failed") return "failed";
        }

    else
        return "problem in accessing the function";
}
假设响应代码的索引为0,作者的索引为1

public string GetAuthorName()
{
string []get_author = SetBookInfo(Id, Name); // returns string[]
if (get_author != null && get_author.Length > 0)
 {
   if(get_author[0].ToLower().Equals("success"))
      return get_author[1];
   else
     return "failed";
  }
else
    return "problem in accessing the function";
}
如果要返回多个字符串,则可以返回
字符串列表

public List<string> GetAuthorName()
{
string []get_author = SetBookInfo(Id, Name); // returns string[]
List<string> list=new List<string>();
if (get_author != null && get_author.Length > 0)
 {
   if(get_author[0].ToLower().Equals("success"))
    {
     list.Add("success"); 
     list.Add(get_author[1]);
    }
   else
     list.Add("failed");
  }
else
    list.Add("problem in accessing the function");
 return list;
}
公共列表GetAuthorName()
{
string[]get_author=SetBookInfo(Id,Name);//返回string[]
列表=新列表();
if(get_author!=null&&get_author.Length>0)
{
if(get_author[0].ToLower().Equals(“success”))
{
列表。添加(“成功”);
添加(获取作者[1]);
}
其他的
列表。添加(“失败”);
}
其他的
添加(“访问函数时出现问题”);
退货清单;
}

代码似乎是正确的。这里到底有什么问题?什么是响应代码索引?@MairajAhmad响应代码索引是0@HarveySpecter我无法进一步说明如何对逻辑进行编码。请引导并返回字符串数组或逗号分隔字符串。该方法的返回类型为
字符串
,而不是
列表
@Shaharyar是,但请查看当响应成功时应返回的内容用户希望返回多个字符串。不,他只想返回
作者的姓名
。我想你有点误会了requirement@MairajAhmad非常感谢您的回复。你能分享你以前的回复吗。返回列表类型。这将在其他情况下对我有所帮助。好的,我将添加它作为编辑,
public List<string> GetAuthorName()
{
string []get_author = SetBookInfo(Id, Name); // returns string[]
List<string> list=new List<string>();
if (get_author != null && get_author.Length > 0)
 {
   if(get_author[0].ToLower().Equals("success"))
    {
     list.Add("success"); 
     list.Add(get_author[1]);
    }
   else
     list.Add("failed");
  }
else
    list.Add("problem in accessing the function");
 return list;
}