I have a function that accepts a string array as a parameter
public static void LogMethodStart(string[] parameters)
{
AddLogEntry("Starting Method", parameters); //this does not work
}
public static void AddLogEntry(string[] parameters)
{
using(StreamWriter sw = new StreamWriter(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), FileName), true))
{
foreach (string p in parameters)
{
stream.WriteLine(p.ToString() + DateTime.Now.ToString());
}
}
}
Is there a way to pass an element to be included as an Array without having to do some Array.resize() operation and check for null etc...?
paramskeyword. stackoverflow.com/q/7580277/97382paramsworks on individual objects, not collections/lists/arraysparamson your existing code without making a few changes. Your description of what you're trying to accomplish is pretty vague so it's tough to be any more specific than that, but bottom line is that if you want to pass a variable number of arguments (of the same type) to a method theparamsis the way to go. It would also be helpful if you explained what yourthis does not workcomment means. Just a quick glance seems like it should be fine.AddLogEntry()insideLogMethodStart(). notice that essentially I'm trying to prepend the array that will be passed intoAddLogEntry()... I am not passing in arguments of the same type, one is an array, one is an object of the type of the array... run my code and see you get the compile error I mentionparamsdon't help to remove null checks as one can still use original syntax to passnull(Foo((string[])null))