I'm trying to understand the design considerations of the team that created the method Environment.GetCommandLineArgs
.
It could have been a static property, very much like System.Web.HttpContext.Current
. After all, the returned value should not change once available. So it's more like a property of the current running process.
I know that any property in .NET is a syntactic sugar to getter/setter methods. But that's the exact reason for using a property rather than an explicit getter method.
Or maybe there is there something I'm missing here?
What do you think?
I suspect it's because it makes a copy of the array each time you call it. For example, consider this program:
using System;
public class Test
{
static void Main(string[] args)
{
string[] argsCopy = Environment.GetCommandLineArgs();
args[0] = "x";
// 0 is the command in this case
argsCopy[1] = "y";
string[] argsCopy2 = Environment.GetCommandLineArgs();
Console.WriteLine(argsCopy2[1]);
}
}
If you run this with "test original" it will still print out "original".
So when you say:
After all, the returned value should not change once available.
Actually, it will return a different value (a new array reference) on every call, precisely because arrays are always mutable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With