Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Environment.GetCommandLineArgs - why is it a method? Why not a property?

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?

like image 848
Ron Klein Avatar asked Mar 08 '12 08:03

Ron Klein


1 Answers

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.

like image 170
Jon Skeet Avatar answered Nov 07 '22 11:11

Jon Skeet