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:
How should I pass an array parameter to this method or similar methods : public XDocument(params Object[] content)?
Is there any other way to create dynamic array in C# without using list and casting to Array?