0

I am trying to understand passing array parameters to methods and creating dynamic arrays in C#. I have 2 questions at the end.


This LINQ to XML statement is working well :

XDocument doc = new XDocument(
    new XElement("Processes",
            from p in Process.GetProcesses()
            orderby p.ProcessName
            select (new XElement("Process", 
                        new XAttribute("Name", p.ProcessName), 
                        new XAttribute("PID", p.Id)))));

I want to rewrite it in a different way to understand passing array concept but it produces an InvalidOperationException at the last line :

var query = from p in Process.GetProcesses()
            orderby p.ProcessName
            select p;

List<XElement> content = new List<XElement>();
foreach (var item in query)
{
    content.Add(new XElement("Process",
        new XAttribute("Name", item.ProcessName),
        new XAttribute("PID", item.Id)));
}

// Is there any other way to create dynamic array instead of using List 
// and converting it to Array?
var paramArr = content.ToArray();
XDocument doc = new XDocument(paramArr);

I have two questions about it:

  1. How should I pass an array parameter to this method or similar methods : public XDocument(params Object[] content)?

  2. Is there any other way to create dynamic array in C# without using list and casting to Array?

Jack Houston
  • 49
  • 11
  • have a look at the example in the msdn : http://msdn.microsoft.com/en-us/library/bb352589.aspx – Tourki Sep 29 '13 at 12:48
  • "but it produces an exception at the last line" WHICH exception? Exceptions have messages that help us to help you. – DasKrümelmonster Sep 29 '13 at 12:50
  • @YAT I already checked these examples. But in first example,it uses static elements, and the second is same with my first statement. How can you change the first example of this link with a dynamic array? – Jack Houston Sep 29 '13 at 12:53
  • @DasKrümelmonster I added the Exception type to the post. – Jack Houston Sep 29 '13 at 12:59

2 Answers2

3

Your second code puts one element "Processes" into XDocument, but second code puts many elements. XDocument can contain zero or one element.

See Valid Content of XElement and XDocument Objects.

Example:

var query = from p in Process.GetProcesses()
            orderby p.ProcessName
            select p;

List<XElement> content = new List<XElement>();
foreach (var item in query)
{
    content.Add(new XElement("Process",
        new XAttribute("Name", item.ProcessName),
        new XAttribute("PID", item.Id)));
}

var paramArr = content.ToArray();
var rootElement = new XElement("Processes", paramArr); // create one root element
XDocument doc = new XDocument(rootElement);

The second example in http://msdn.microsoft.com/en-us/library/bb352589.aspx puts one element and one comment, not two elements.

unarist
  • 616
  • 8
  • 26
0

For your first question, have a look in this question. Documentation is here. I think you problem may be solved by casting the array to object:

XDocument doc = new XDocument((object)paramArr);

Second question: No. Arrays in C# have a constant length.

Community
  • 1
  • 1
DasKrümelmonster
  • 5,816
  • 1
  • 24
  • 45