0

I have seen a sample C# code that looked like this:

private static string GetCommandLine(Process process)
{
    string cmd = "";
    using (var s = new ManagementObjectSearcher("SELECT CommandLine FROM Win32_Process WHERE ProcessId = " + process.Id))
    {
        foreach (var @object in s.Get())
        {
            if (cmd.Length > 0) cmd += " ";
            cmd += @object["CommandLine"];
        }
    }
    return cmd;
}

What is the purpose of the @ in the loop variable?

Alex
  • 583
  • 5
  • 18
  • 1
    `object` is a keyword in C#. Variables can not have same name as keywords. You need use `@` prefix to if you want to use keywords as variable names. – Chetan Mar 29 '17 at 22:55

1 Answers1

1

If you want to use reserved words as variable names, you prefix them with the @ sign.

Hanno
  • 467
  • 11
  • 21
  • It never occurred to me that it was part of the variable name. I though it was an operator :-) – Alex Mar 29 '17 at 22:58