views:

73

answers:

1

I know that prefixing a C# string literal with @ marks it as a verbatim string literal, but what purpose would this serve if it was prefixed to the name of a private property?

This came up in some code I was maintaining...

public class JourneyBuilder
{
    // ...

    private JourneyBuilder @this
    {
        get { return this; }
    }

    // ...
}

Can anyone suggest a rationale for this?

Edit

I guess this is actually two questions (one of which has already been answered). The second is: Why have a private property that just returns this?

+3  A: 

It is a way of "escaping" a reserved keyword so you can use it as an identifier. For the most part it is better to simply come up with a different name rather than to use this approach, but the C# compiler does allow this.

On a side note the only place I have seen it used where it makes a bit of sense is when you are naming the instance parameter of an extension method like this:

public static void Foo(this Object @this)
{
    // ...
}

But even in this case it would be just as good to name the parameter source or something equally as indicative of its nature.

As to why you would ever need a private property that returns this: there is no reason to ever do it. You are only providing a private method to the type for accessing a reference that is already available. Fortunately, since the member is private, you can easily remove it.

Andrew Hare
This can come up when working with libraries created in another language (IIRC this is not a keyword in VB). The CLS rules, however, try to block this by reserving a union of language keyword sets.
Richard
But the second question remains - why have a private property (regardless of name) that just returns this?
Richard Ev