C# 如何在c中调用返回类型为array的函数

C# 如何在c中调用返回类型为array的函数,c#,C#,我在页面加载时调用此函数 即字符串[]arr=GetCDCatalog; 但这是一个错误,无法将类型“Calender.CD[]”隐式转换为“string[]” 请建议如何在返回类型为数组的页面加载上调用函数 您的方法被声明为返回CD[],并且正如编译器告诉您的,您不能将CD[]转换为字符串[]。可以这样称呼: public CD[] GetCDCatalog() { XDocument docXML = XDocument.Load(Server.MapPath("mydata

我在页面加载时调用此函数 即字符串[]arr=GetCDCatalog; 但这是一个错误,无法将类型“Calender.CD[]”隐式转换为“string[]”
请建议如何在返回类型为数组的页面加载上调用函数

您的方法被声明为返回CD[],并且正如编译器告诉您的,您不能将CD[]转换为字符串[]。可以这样称呼:

public CD[] GetCDCatalog()
{
    XDocument docXML =
    XDocument.Load(Server.MapPath("mydata.xml"));

    var CDs =
      from cd in docXML.Descendants("Table")
      select new CD
      {
          title = cd.Element("title").Value,
          star = cd.Element("star").Value,
          endTime = cd.Element("endTime").Value,

      };
    return CDs.ToArray<CD>();
}
如果需要转换为字符串数组,则可以使用如下内容:

CD[] cds = GetCDCatalog();
或者,如果阵列中不需要它,可以使用:

string[] cds = GetCDCatalog().Select(x => x.title).ToArray();

您的方法被声明为返回CD[],并且正如编译器告诉您的,您不能将CD[]转换为字符串[]。可以这样称呼:

public CD[] GetCDCatalog()
{
    XDocument docXML =
    XDocument.Load(Server.MapPath("mydata.xml"));

    var CDs =
      from cd in docXML.Descendants("Table")
      select new CD
      {
          title = cd.Element("title").Value,
          star = cd.Element("star").Value,
          endTime = cd.Element("endTime").Value,

      };
    return CDs.ToArray<CD>();
}
如果需要转换为字符串数组,则可以使用如下内容:

CD[] cds = GetCDCatalog();
或者,如果阵列中不需要它,可以使用:

string[] cds = GetCDCatalog().Select(x => x.title).ToArray();

将Page.Load中的调用更改为Calender.CD[]arr=GetCDCatalog

或使用列表:

IEnumerable<string> cds = GetCDCatalog().Select(x => x.title);

将Page.Load中的调用更改为Calender.CD[]arr=GetCDCatalog

或使用列表:

IEnumerable<string> cds = GetCDCatalog().Select(x => x.title);

问题是您将其称为:

public List<CD> GetCDCatalog() { XDocument docXML = XDocument.Load(Server.MapPath("mydata.xml"));

    var CDs =
      from cd in docXML.Descendants("Table")
      select new CD
      {
          title = cd.Element("title").Value,
          star = cd.Element("star").Value,
          endTime = cd.Element("endTime").Value,

      };
    return CDs.ToList();
}
当GetCDCatalog返回CD[]数组时

您需要执行以下操作:

string[] arr = GetCDCatalog();

问题是您将其称为:

public List<CD> GetCDCatalog() { XDocument docXML = XDocument.Load(Server.MapPath("mydata.xml"));

    var CDs =
      from cd in docXML.Descendants("Table")
      select new CD
      {
          title = cd.Element("title").Value,
          star = cd.Element("star").Value,
          endTime = cd.Element("endTime").Value,

      };
    return CDs.ToList();
}
当GetCDCatalog返回CD[]数组时

您需要执行以下操作:

string[] arr = GetCDCatalog();

我想你的错误在呼叫代码中。您正在执行类似字符串[]cds=GetCDCatalog;?我想你的错误在呼叫代码中。您正在执行类似字符串[]cds=GetCDCatalog;?最好使用接口作为返回类型,如IList或ICollection。最好使用接口作为返回类型,如IList或ICollection。