typedef

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mr.Tickle

    typedef

    I want to give an alias for a type in C# so i can say SOMETYPEID refers to
    int etc, how can I do this on C#?

    something like typedef long SOMETYPEID in c++


  • Leo Lin

    #2
    Re: typedef

    the "using directive"
    for example

    class A{
    }

    using B = A;
    B b;

    "Mr.Tickle" <MrTickle@mrmen .com> wrote in message
    news:eEbOMI5gDH A.2400@TK2MSFTN GP11.phx.gbl...[color=blue]
    > I want to give an alias for a type in C# so i can say SOMETYPEID refers[/color]
    to[color=blue]
    > int etc, how can I do this on C#?
    >
    > something like typedef long SOMETYPEID in c++
    >
    >[/color]


    Comment

    • 100

      #3
      Re: typedef

      Hi Mr. Tickle,

      you can use *using* directive
      using SOMETYPEID = System.Int32;

      Because c# doesn't have anything like header files and this alises are not
      embeded as a metadata in the assemblies you can use it only in the file
      where you put this declaration.

      HTH
      B\rgds
      100


      "Mr.Tickle" <MrTickle@mrmen .com> wrote in message
      news:eEbOMI5gDH A.2400@TK2MSFTN GP11.phx.gbl...[color=blue]
      > I want to give an alias for a type in C# so i can say SOMETYPEID refers[/color]
      to[color=blue]
      > int etc, how can I do this on C#?
      >
      > something like typedef long SOMETYPEID in c++
      >
      >[/color]


      Comment

      • Daniel Pratt

        #4
        Re: typedef

        Hi Mr. Tickle,

        "Mr.Tickle" <MrTickle@mrmen .com> wrote in message
        news:eEbOMI5gDH A.2400@TK2MSFTN GP11.phx.gbl...[color=blue]
        > I want to give an alias for a type in C# so i can say SOMETYPEID refers[/color]
        to[color=blue]
        > int etc, how can I do this on C#?
        >
        > something like typedef long SOMETYPEID in c++[/color]

        There is no typedef per se. You could do something like this, though:

        using SOMETYPEID = System.Int64;

        The primary drawback to the above is that it must be included in every
        code file where you would want to use SOMETYPEID.

        Regards,
        Dan


        Comment

        • Nicholas Paldino [.NET/C# MVP]

          #5
          Re: typedef

          Mr.Tickle,

          You can do this using the "using" directive, like so:

          // Indicate sometypeid is an int.
          using SOMETYPEID = int;

          Or:

          using SOMETYPEID = System.Int32;

          However, this is different from a typedef in C++, as it doesn't create
          new type information. It is just an alias for the type.

          Hope this helps.


          --
          - Nicholas Paldino [.NET/C# MVP]
          - nick(dot)paldin o=at=exisconsul ting<dot>com



          "Mr.Tickle" <MrTickle@mrmen .com> wrote in message
          news:eEbOMI5gDH A.2400@TK2MSFTN GP11.phx.gbl...[color=blue]
          > I want to give an alias for a type in C# so i can say SOMETYPEID refers[/color]
          to[color=blue]
          > int etc, how can I do this on C#?
          >
          > something like typedef long SOMETYPEID in c++
          >
          >[/color]


          Comment

          • Mr.Tickle

            #6
            Re: typedef

            ok, thanks.

            Anotherone, is it better to use int or System.Int32 in code? using the built
            in keyword or the library type?

            kind like using int or INT in C++

            "Nicholas Paldino [.NET/C# MVP]" <nicholas.paldi no@exisconsulti ng.com> wrote
            in message news:#S$Umv5gDH A.1340@tk2msftn gp13.phx.gbl...[color=blue]
            > Mr.Tickle,
            >
            > You can do this using the "using" directive, like so:
            >
            > // Indicate sometypeid is an int.
            > using SOMETYPEID = int;
            >
            > Or:
            >
            > using SOMETYPEID = System.Int32;
            >
            > However, this is different from a typedef in C++, as it doesn't create
            > new type information. It is just an alias for the type.
            >
            > Hope this helps.
            >
            >
            > --
            > - Nicholas Paldino [.NET/C# MVP]
            > - nick(dot)paldin o=at=exisconsul ting<dot>com
            >
            >
            >
            > "Mr.Tickle" <MrTickle@mrmen .com> wrote in message
            > news:eEbOMI5gDH A.2400@TK2MSFTN GP11.phx.gbl...[color=green]
            > > I want to give an alias for a type in C# so i can say SOMETYPEID refers[/color]
            > to[color=green]
            > > int etc, how can I do this on C#?
            > >
            > > something like typedef long SOMETYPEID in c++
            > >
            > >[/color]
            >
            >[/color]


            Comment

            • Nicholas Paldino [.NET/C# MVP]

              #7
              Re: typedef

              Mr. Tickle,

              It really doesn't matter, as it is just an alias. They are the same thing.

              -Nicholas Paldino [.NET/C# MVP]
              -nick(dot)paldin o=at=exisconsul ting<dot>com

              On Thu, 25 Sep 2003 21:51:35 +0200, Mr.Tickle <MrTickle@mrmen .com> wrote:
              [color=blue]
              > ok, thanks.
              >
              > Anotherone, is it better to use int or System.Int32 in code? using the built
              > in keyword or the library type?
              >
              > kind like using int or INT in C++
              >
              > "Nicholas Paldino [.NET/C# MVP]" <nicholas.paldi no@exisconsulti ng.com> wrote
              > in message news:#S$Umv5gDH A.1340@tk2msftn gp13.phx.gbl...[color=green]
              >> Mr.Tickle,
              >>
              >> You can do this using the "using" directive, like so:
              >>
              >> // Indicate sometypeid is an int.
              >> using SOMETYPEID = int;
              >>
              >> Or:
              >>
              >> using SOMETYPEID = System.Int32;
              >>
              >> However, this is different from a typedef in C++, as it doesn't create
              >> new type information. It is just an alias for the type.
              >>
              >> Hope this helps.
              >>
              >>
              >> --
              >> - Nicholas Paldino [.NET/C# MVP]
              >> - nick(dot)paldin o=at=exisconsul ting<dot>com
              >>
              >>
              >>
              >> "Mr.Tickle" <MrTickle@mrmen .com> wrote in message
              >> news:eEbOMI5gDH A.2400@TK2MSFTN GP11.phx.gbl...[color=darkred]
              >> > I want to give an alias for a type in C# so i can say SOMETYPEID refers[/color]
              >> to[color=darkred]
              >> > int etc, how can I do this on C#?
              >> >
              >> > something like typedef long SOMETYPEID in c++
              >> >
              >> >[/color]
              >>
              >>[/color]
              >
              >
              >[/color]


              Comment

              • 100

                #8
                Re: typedef

                Hi, Mr.Tickle[color=blue]
                > Anotherone, is it better to use int or System.Int32 in code? using the[/color]
                built[color=blue]
                > in keyword or the library type?[/color]

                You can use either. *int* is just language keyword that serves as a shortcut
                for System.Int32.
                Using *int* will make the code nicer and easier for reading by any C#
                programmer.
                You may consider using System.Int32, though, if you share the code with
                someone that work with another language. This guy may has got use to use
                different keyword for integer values or in some of the languages *int* may
                mean something else like System.Int64 for example and this may put him/her
                to confusion. Any .NET programmer has to know what System.Int32 is, though.

                Other examples may be found of advantages and disadvantages of using *int*
                and using System.Int32.
                I think you have to use the one that makes your work and the work of the
                other members of your team easier and safer.

                HTH
                B\rgds
                100


                Comment

                • Frank Oquendo

                  #9
                  Re: typedef

                  Thus spake Mr.Tickle:
                  [color=blue]
                  > Anotherone, is it better to use int or System.Int32 in code? using
                  > the built in keyword or the library type?[/color]

                  You should use the alias defined by your language and let the copiler
                  handle the mapping to the appropriate type within the Framework.

                  --
                  There are 10 kinds of people. Those who understand binary and those who
                  don't.




                  Comment

                  • Jon Skeet

                    #10
                    Re: typedef

                    Mr.Tickle <MrTickle@mrmen .com> wrote:[color=blue]
                    > Anotherone, is it better to use int or System.Int32 in code? using the built
                    > in keyword or the library type?[/color]

                    I personally use int unless it's part of a member name, where I'll use
                    the framework name, as it could be invoked from a different language.

                    For instance, I might have:

                    int ReadInt32();
                    float ReadSingle();

                    etc

                    --
                    Jon Skeet - <skeet@pobox.co m>
                    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                    If replying to the group, please do not mail me too

                    Comment

                    • Chris LaJoie

                      #11
                      Re: typedef

                      you can't inherit from a base type in C#, but you can from almost any other
                      type.

                      public class al : ArrayList {}

                      with this, you could just use 'al' in place of 'ArrayList' if you were
                      declaring that type. Other than that, I don't believe there's an
                      equivelant.

                      Chris

                      "Mr.Tickle" <MrTickle@mrmen .com> wrote in message
                      news:eEbOMI5gDH A.2400@TK2MSFTN GP11.phx.gbl...[color=blue]
                      > I want to give an alias for a type in C# so i can say SOMETYPEID refers[/color]
                      to[color=blue]
                      > int etc, how can I do this on C#?
                      >
                      > something like typedef long SOMETYPEID in c++
                      >
                      >[/color]


                      Comment

                      • Jon Skeet

                        #12
                        Re: typedef

                        Chris LaJoie <chris@etriptra der.com> wrote:[color=blue]
                        > you can't inherit from a base type in C#, but you can from almost any other
                        > type.[/color]

                        Could you clarify what you mean by a "base type" here? I suspect you
                        mean "value type" but it's not terrible clear.

                        --
                        Jon Skeet - <skeet@pobox.co m>
                        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                        If replying to the group, please do not mail me too

                        Comment

                        • Chris LaJoie

                          #13
                          Re: typedef

                          Yes, base type was a poor choice of words. Value type, such as int, long,
                          char, etc. is what I meant.

                          Chris

                          "Jon Skeet" <skeet@pobox.co m> wrote in message
                          news:MPG.19df3d fc165f705c98977 a@msnews.micros oft.com...[color=blue]
                          > Chris LaJoie <chris@etriptra der.com> wrote:[color=green]
                          > > you can't inherit from a base type in C#, but you can from almost any[/color][/color]
                          other[color=blue][color=green]
                          > > type.[/color]
                          >
                          > Could you clarify what you mean by a "base type" here? I suspect you
                          > mean "value type" but it's not terrible clear.
                          >
                          > --
                          > Jon Skeet - <skeet@pobox.co m>
                          > http://www.pobox.com/~skeet
                          > If replying to the group, please do not mail me too[/color]


                          Comment

                          • codymanix

                            #14
                            Re: typedef

                            "Chris LaJoie" <chris@etriptra der.com> schrieb im Newsbeitrag
                            news:eV7zcqIhDH A.2212@TK2MSFTN GP09.phx.gbl...[color=blue]
                            > you can't inherit from a base type in C#, but you can from almost any[/color]
                            other[color=blue]
                            > type.
                            >
                            > public class al : ArrayList {}
                            >
                            > with this, you could just use 'al' in place of 'ArrayList' if you were
                            > declaring that type. Other than that, I don't believe there's an
                            > equivelant.[/color]

                            the problem is that this approach does not work on value types.
                            but i never understood why they didn't allow inheritance of value types

                            --
                            cody

                            [Freeware, Games and Humor]
                            www.deutronium.de.vu || www.deutronium.tk


                            Comment

                            • Jon Skeet

                              #15
                              Re: typedef

                              codymanix <NO.SPAM.deutro nium@gmx.net> wrote:[color=blue]
                              > the problem is that this approach does not work on value types.
                              > but i never understood why they didn't allow inheritance of value types[/color]

                              Consider if you could derive from value types. Suddenly they wouldn't
                              have a fixed size - an array declared as:

                              MyStruct[] x = new MyStruct[100];

                              couldn't just allocate 100*(size of MyStruct) because it could hold
                              MyDerivedStruct elements which were larger than MyStruct ones.

                              Now propagate that problem everywhere else - parameter passing, stack
                              sizing, etc.

                              --
                              Jon Skeet - <skeet@pobox.co m>
                              Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                              If replying to the group, please do not mail me too

                              Comment

                              Working...