C# 在C中的函数中返回两个字符串

C# 在C中的函数中返回两个字符串,c#,return,C#,Return,考虑: protected string Active_Frozen(string text, string color) { connection(); string query = "SELECT CustomerInfo FROM ActiveSubscription WHERE UserName=@UserName"; SqlCommand cmd = new SqlCommand(query, conn); if(query=="true")

考虑:

protected string Active_Frozen(string text, string color)
{
    connection();
    string query = "SELECT CustomerInfo FROM ActiveSubscription WHERE UserName=@UserName";

    SqlCommand cmd = new SqlCommand(query, conn);

    if(query=="true")
    {
        text = "Active";
        color = "Green";
    }
    else
    {
        text = "Frozen";
        color= "Red";
    }

    return (text, color);
}
我想返回两个字符串:文本和颜色,但我不确定问题出在哪里

返回语句时出错:

参数?文字/颜色

无法将lambda表达式转换为类型“string”,因为它不是委托类型

您可以使用out参数:

protected string Active_Frozen(out string text, out string color)
{
    connection();

    string query = "SELECT CustomerInfo FROM ActiveSubscription WHERE UserName=@UserName";

    SqlCommand cmd = new SqlCommand(query, conn);


    if(query=="true")
    {
       text = "Active";
       color = "Green";
    }

    else
    {
       text = "Frozen";
       color= "Red";
    }
}
可以这样称呼:

string text;
string color;

Active_Frozen(out text, out color);

生成一个类并从方法返回一个类对象:

public class Container
{
    public string text {get;set;}
    public string color{get;set;}
}
方法:

protected Container Active_Frozen(string text, string color)
{
    connection();

    string query = "SELECT CustomerInfo FROM ActiveSubscription WHERE UserName=@UserName";

    SqlCommand cmd = new SqlCommand(query, conn);


    if(query=="true")
    {
        Container c = new Container{text = "Frozen", color= "Red"};
    }

    else
    {
        Container c = new Container{text = "Frozen", color= "Red"};
    }

    return c;
}

当您返回两个对象时,需要将函数声明为返回两个对象。但是,函数声明为返回一个字符串

解决此问题的一种方法是使用元组:


请注意,返回颜色名称而不是颜色对象本身可能并不理想,具体取决于在设计中使用的返回值。如果您希望返回颜色的对象表示形式而不是字符串,请更改元组的第二个类型参数,或者创建自己的类来表示文本及其颜色。

您必须返回数组、元组,甚至是知道的列表,有朝一日您可能必须返回更多项

protected string[] Active_Frozen(string text, string color)
{
    // Code code
    return new string[] {text, color};
}

protected Tuple<string, string> Active_Frozen(string text, string color)
{
    // Code code
    return new Tuple<string, string>(text, color);
}

protected List<string> Active_Frozen(string text, string color)
{
    List toReturn = new List<string>();

    // Code code

    toReturn.Add(text);
    toReturn.Add(color);
    return toReturn;
}

我认为您可以使用字符串列表,并且可以返回您想要的所有值:

protected list<string> Active_Frozen(string text, string color)
{
    connection();
    string query = "SELECT CustomerInfo FROM ActiveSubscription WHERE UserName=@UserName";

    SqlCommand cmd = new SqlCommand(query, conn);

    if(query=="true")
    {
        text = "Active";
        color = "Green";
    }
    else
    {
        text = "Frozen";
        color= "Red";
    }
    list<string> liststring = new list<string> {text, color};
    return liststring;

}

实现这一目标有多种方法:

排列

结构类似于类对象

struct myReturnValues
{
    public string text;
    public string color;
}

protected myReturnValues Active_Frozen(string text, string color)
{
    myReturnValues returnVal = new myReturnValues();
    returnVal.text = text;
    returnVal.color = color;
    return returnVal;
}
输出参数

protected myReturnValues Active_Frozen(out string text, out string color)
{
    text="new valuess";
    color= "new color";
}

将属性文本和颜色封装在类中,并返回该类的实例。您描述的语法在C中不可用。您可以返回一个元组,或者最好只滚动一个包含两个字符串作为属性的简单类并返回它。编辑:或使用out或ref参数,但使用起来可能有点痛苦。您需要将函数声明为声明两件事:
protected string[] Active_Frozen(string text, string color)
{
    string [] returnVal=new string[2];
    returnVal[0] = text;
    returnVal[1] = color;
    return returnVal;
}
struct myReturnValues
{
    public string text;
    public string color;
}

protected myReturnValues Active_Frozen(string text, string color)
{
    myReturnValues returnVal = new myReturnValues();
    returnVal.text = text;
    returnVal.color = color;
    return returnVal;
}
protected myReturnValues Active_Frozen(out string text, out string color)
{
    text="new valuess";
    color= "new color";
}