【问题标题】:Returning more than one value from a method in c# [duplicate]从c#中的方法返回多个值[重复]
【发布时间】:2014-04-04 20:34:58
【问题描述】:

我对从方法返回两个值有疑问。

我有一个方法,我必须从中返回两个值。我确实喜欢这个。

public string fileName(string rate,out string xmlName,out string xsdName)
{
    if(rate=="2013")
    {
       xmlName="abc";
       xsdName="def";
    }

    if(rate=="2014")
    {
       xmlName="pqr";
       xsdName="xyz";
    }

    //stmt 1 (Here it is asking to give default values for xmlname and xsdName.But i dont want to give any default values.)
}

现在在另一个类中,我必须调用此函数并在该类中分配xmlnamexsdName 的这些值。我该怎么做?

【问题讨论】:

  • 如果您的方法必须返回多个对象,请使用包含数据的类或使用两种方法。重新思考你的逻辑!

标签: c#


【解决方案1】:
public OtherClass fileName(string rate)
{
    OtherClass obj = new OtherClass();
    if (rate == "2013")
    {
        obj.xmlName = "abc";
        obj.xsdName = "def";
    }
    if (rate == "2014")
    {
        obj.xmlName = "pqr";
        obj.xsdName = "xyz";
    }
    return obj;
}

public class OtherClass
{
    public string xmlName { get; set; }
    public string xsdName { get; set; }
}

【讨论】:

    【解决方案2】:

    你会这样使用它:

    string xmlName;
    string xsdName;
    fileName("2014", out xmlName, out xsdName);
    

    您的 fileName() 方法的返回类型可以是 void,因为从技术上讲,您并没有从该函数返回任何内容。

    【讨论】:

      【解决方案3】:

      最好的概念是使用面向目标的编程。创建一个具有两个属性的类,xmlNamexsdName。然后从方法中返回一个新的类实例。

      下面的代码应该会给你一个想法。

      文件XmlFile.cs中的类实现

      public class XmlFile
      {
          public string XmlName
          { get; set; }
          public string XsdName
          { get: set; }
      }
      

      功能实现:

      public XmlFile fileName(string rate)
      {
          XmlFile file = new XmlFile();
      
          if (rate == "2013")
          {
              file.XmlName = "abc";
              file.XsdName = "def";
          }
      
          if (rate == "2014")
          {
              file.XmlName = "pqr";
              file.XsdName = "xyz";
          }
      
          return file;
      }
      

      请详细查看 OO 编程,C# 需要。

      【讨论】:

        【解决方案4】:

        你的问题很模糊,但我会尽力而为。

        下面是如何调用fileName 并接收最后两个参数的输出:

        string xmlName;
        string xsdName;
        
        myInstance.fileName("2014", out xmlName, out xsdName);
        

        我倾向于回避使用out。更好的解决方案是创建一个包装数据的新类:

        public class File
        {
            public File(string fileName, string xmlName, string xsdName)
            {
                FileName = fileName;
                XmlName = xmlName;
                XsdName = xsdName;
            }
        
            public string FileName
            {
                get;
                private set;
            }
        
            public string XmlName
            {
                get;
                private set;
            }
        
            public string XsdName
            {
                get;
                private set;
            }
        }
        
        public class OtherClass
        {
            public File FileName(string rate)
            {
                switch (rate)
                {
                    case "2013":
                        return new File(..., "abc", "def");
                    case "2014":
                        return new File(..., "pqr", "xyz");
                    default:
                        throw new ArgumentException(String.Format("Unexpected rate '{0}'.", rate)); // Or, simply return null
                }
            }
        }
        

        【讨论】:

          【解决方案5】:

          调用带有out参数的函数:

          string xmlN;
          string xsdN;
          var result = fileName("rate value", out xmlN, out xsdN);
          

          但一个问题是您需要在函数结束之前分配它们。 在这种情况下,将它们设置为 null 可能是合适的。

          【讨论】:

            【解决方案6】:

            有几种可能的方法来做到这一点。首先是你是怎么做的——输出参数

            string xmlName, xsdName;
            filename("xxx", out xmlName, out xsdName);
            

            第二个是使用元组。

            public Tuple<string, string> fileName(string rate) 
            {
                ...
                return Tuple.Create(xmlName, xsdName)
            }
            

            第三个是定义你自己的类

            class XmlInfo
            {
                public string XmlName {get; set;}
                public string XsdName {get; set;}
            }
            
            
            XmlInfo filename(string rate)
            {
                ...
                return new XmlInfo() { XmlName = xmlName, XsdName = xsdName };
            }
            

            【讨论】:

              猜你喜欢
              • 2012-09-20
              • 2010-12-14
              • 1970-01-01
              • 2018-08-27
              • 2016-01-18
              • 2015-06-26
              • 2019-07-29
              • 2013-06-18
              • 1970-01-01
              相关资源
              最近更新 更多