using of "@" before classname

  • Thread starter Thread starter Abhi
  • Start date Start date
A

Abhi

i was looking in some code in which i found that it is using @ keyword before
class name and constructor like this

public class @MyClass
{
public @MyClass
{
}
}

can anyone please help me to know that when and why we use this keyword
before classname.

Thanks
Abhi
 
Abhi said:
i was looking in some code in which i found that it is using @ keyword before
class name and constructor like this

public class @MyClass
{
public @MyClass
{
}
}

can anyone please help me to know that when and why we use this keyword
before classname.

Thanks
Abhi

If, for some reason, you need/want to use a reserved word as an
identifier, you need to prefix it with @

Try:

Int32 @this = 0;
Int32 this = 0;

and you'll notice that the compiler will complain about the second
declaration since "this" is a reserved word.
 
i was looking in some code in which i found that it is using @ keyword before
class name and constructor like this

public class @MyClass
{
    public @MyClass
   {
   }

}

can anyone please help me to know that when and why we use this keyword
before classname.

Thanks
Abhi

Hi,

FRankly I have never seen this before, where did you see it ?
 
i was looking in some code in which i found that it is using @ keyword before
class name and constructor like this

public class @MyClass
{
    public @MyClass
   {
   }

}

can anyone please help me to know that when and why we use this keyword
before classname.

Thanks
Abhi

It allows reserved words to be used as identifiers. It's use in C# is
not that common. The reasoning behind that is that all keywords in C#
begin with a lowercase character and if you follow the .NET naming
guidelines you would make all public API identifiers begin with a
capital letter. So even if your API was developed in another language
it's still not going to be likely that you encounter an identifier-
keyword collision in C#.

It's a lot more likely that you would need to use VB.NET's equivalent
[] characters to surround identifiers since VB is not case sensitive.
For example, I could declare a class called Stop in C# not knowing
that it was a keyword in VB.NET forcing developers of VB.NET to use
[]'s if they wanted to use my class.
 
If, for some reason, you need/want to use a reserved word as an
identifier, you need to prefix it with @

But it would be a much better idea to choose another name.

Arne
 
Brian said:
You might not have that option. See my other post on this thread.

If the API is given by someone else (possible not coding in C#), then
another name may not be an option.

But given the reserved words in C# are what I would call "relative
common" reserved words, then I would say the chance is low, but there
are a few candidates: lock, event, fixed, yield, checked !

Arne
 
You can see the IDE creating this for you by simply adding a new class using
IDE.

For example, right click on the project, select Add > Class... when prompted
to enter a name for the class choose a name such as "this" or "int". Click
ok and you will see the designer adding the @ character at the beginning of
the class name (the file name will not be affected).


message
i was looking in some code in which i found that it is using @ keyword
before
class name and constructor like this

public class @MyClass
{
public @MyClass
{
}

}

can anyone please help me to know that when and why we use this keyword
before classname.

Thanks
Abhi

Hi,

FRankly I have never seen this before, where did you see it ?
 
But given the reserved words in C# are what I would call "relative
common" reserved words, then I would say the chance is low, but there
are a few candidates: lock, event, fixed, yield, checked !

Even in those cases it's still not likely as long as the API developer
followed the naming guideline of capitalizing the first character of
an identifier. I have been doing some VB.NET lately and used a
library written in C# that really did have an Event class. Well,
Event is a keyword in VB.NET so I have to surround it with
[]'s...annoying.
 
Brian said:
Even in those cases it's still not likely as long as the API developer
followed the naming guideline of capitalizing the first character of
an identifier.

Not likely.

But not as unlikely as it may appear - because "not knowing C# keywords"
and "not knowing naming guideline" is undoubtedly correlated.

Arne
 
Back
Top