C# prefixing parameter names with @

C#

C# Problem Overview


> Possible Duplicate:
> What does the @ symbol before a variable name mean in C#?

>###Duplicate: >What does the @ symbol before a variable name mean in C#?

Sometimes I see some C# code where a method-parameter is prefixed with an @, like this:

public static void SomeStaticMethod( SomeType @parameterName ) { }

What is the meaning of this ? Does it has some significant special meaning ?

I am creating an EventListener in NHibernate, and when I let VS.NET generate the interface methods, it generates the OnPostLoad method like this:

public class PostLoadEventListener : IPostLoadEventListener
{
    public void OnPostLoad( PostLoadEvent @event )
    {
        
    }
}

Why is this ?

C# Solutions


Solution 1 - C#

Try and make a variable named class and see what happens -- You'll notice you get an error.

This lets you used reserved words as variable names.

Unrelated, you'll also notice strings prefixed with @ as well -- This isn't the same thing...

string says = @"He said ""This literal string lets me use \ normally 
    and even line breaks"".";

This allows you to use 'literal' value of the string, meaning you can have new lines or characters without escapes, etc...

Solution 2 - C#

The @ prefix allows you to use reserved words like class, interface, events, etc as variable names in C#. So you can do

int @int = 1

Solution 3 - C#

event is a C# keyword, the @ is an escape character that allows you to use a keyword as a variable name.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFrederik GheyselsView Question on Stackoverflow
Solution 1 - C#hugowareView Answer on Stackoverflow
Solution 2 - C#RadView Answer on Stackoverflow
Solution 3 - C#Timothy CarterView Answer on Stackoverflow