using of "@" before classname

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?QWJoaQ==?=

    using of "@" before classname

    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
  • =?UTF-8?B?TGFzc2UgVsOlZ3PDpnRoZXIgS2FybHNlbg==?=

    #2
    Re: using of "@&quot ; before classname

    Abhi wrote:
    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.

    --
    Lasse Vågsæther Karlsen
    mailto:lasse@vk arlsen.no
    Blogger ist ein Veröffentlichungs-Tool von Google, mit dem du ganz einfach deine Gedanken der Welt mitteilen kannst. Mit Blogger kannst du problemlos Texte, Fotos und Videos in deinem persönlichen Blog oder deinem Team-Blog veröffentlichen.

    PGP KeyID: 0xBCDEA2E3

    Comment

    • Ignacio Machin ( .NET/ C# MVP )

      #3
      Re: using of "@&quot ; before classname

      On May 28, 6:47 am, Abhi <A...@discussio ns.microsoft.co mwrote:
      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 ?

      Comment

      • Brian Gideon

        #4
        Re: using of &quot;@&quot ; before classname

        On May 28, 5:47 am, Abhi <A...@discussio ns.microsoft.co mwrote:
        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.

        Comment

        • =?UTF-8?B?QXJuZSBWYWpow7hq?=

          #5
          Re: using of &quot;@&quot ; before classname

          Lasse Vågsæther Karlsen wrote:
          Abhi wrote:
          >i was looking in some code in which i found that it is using @ keyword
          >before class name and constructor like this
          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

          Comment

          • Brian Gideon

            #6
            Re: using of &quot;@&quot ; before classname

            On May 28, 9:11 pm, Arne Vajhøj <a...@vajhoej.d kwrote:
            But it would be a much better idea to choose another name.
            >
            Arne
            You might not have that option. See my other post on this thread.

            Comment

            • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

              #7
              Re: using of &quot;@&quot ; before classname

              Brian Gideon wrote:
              On May 28, 9:11 pm, Arne Vajhøj <a...@vajhoej.d kwrote:
              >But it would be a much better idea to choose another name.
              >
              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

              Comment

              • Rene

                #8
                Re: using of &quot;@&quot ; before classname

                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).


                "Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin @gmail.comwrote in
                message
                news:b1f33bdf-e3cd-41d4-bcf8-4e1b06f03b69@h1 g2000prh.google groups.com...
                On May 28, 6:47 am, Abhi <A...@discussio ns.microsoft.co mwrote:
                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 ?

                Comment

                • Brian Gideon

                  #9
                  Re: using of &quot;@&quot ; before classname

                  On May 28, 9:57 pm, Arne Vajhøj <a...@vajhoej.d kwrote:
                  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.

                  Comment

                  • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

                    #10
                    Re: using of &quot;@&quot ; before classname

                    Brian Gideon wrote:
                    On May 28, 9:57 pm, Arne Vajhøj <a...@vajhoej.d kwrote:
                    >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.
                    Not likely.

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

                    Arne

                    Comment

                    Working...