Difference between string and String?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ronald S. Cook

    Difference between string and String?

    I notice there are many terms in the .NET Framework like string and String.
    What's the difference? Which should I use?

    I also see int and Int32. They are the same?

    Thanks,
    Ron


  • Arne Vajhøj

    #2
    Re: Difference between string and String?

    Ronald S. Cook wrote:
    I notice there are many terms in the .NET Framework like string and String.
    What's the difference? Which should I use?
    >
    I also see int and Int32. They are the same?
    They are the same.

    int and string are types in the C# language.

    Int32 and String are types in the .NET framework.

    And the C# types are mapped to the .NET types.

    My wording. MS may explain it differently.

    Arne

    Comment

    • Michael

      #3
      Re: Difference between string and String?

      *"Ronald S. Cook" <rcook@westinis .comwrote in message
      news:eEr66ZWzGH A.4920@TK2MSFTN GP06.phx.gbl...
      *I notice there are many terms in the .NET Framework like string and String.
      * What's the difference? Which should I use?
      *
      * I also see int and Int32. They are the same?
      *
      * Thanks,
      * Ron
      *
      *

      Ron,

      They're the same.
      String comes from the .NET class System.String and string (lower case) is a
      C# alias for System.String.
      I often see devs interchange the .NET and C# aliases for various data types;
      not sure why though.

      -MH


      Comment

      • clintonG

        #4
        Re: Difference between string and String?

        They do seem to be different...
        The Split method is not accessible when trying to use it as string.Split but
        it is as String.Split
        If they are the same how could that be?


        <%= Clinton Gallagher
        NET csgallagher AT metromilwaukee. com
        URL http://clintongallagher.metromilwaukee.com/
        MAP 43°2'17"N 88°2'37"W : 43°2'17"N 88°2'37"W

        "Michael" <mhugh@domain.c omwrote in message
        news:%237ZMgiWz GHA.4968@TK2MSF TNGP05.phx.gbl. ..
        *"Ronald S. Cook" <rcook@westinis .comwrote in message
        news:eEr66ZWzGH A.4920@TK2MSFTN GP06.phx.gbl...
        *I notice there are many terms in the .NET Framework like string and
        String.
        * What's the difference? Which should I use?
        *
        * I also see int and Int32. They are the same?
        *
        * Thanks,
        * Ron
        *
        *
        >
        Ron,
        >
        They're the same.
        String comes from the .NET class System.String and string (lower case) is
        a C# alias for System.String.
        I often see devs interchange the .NET and C# aliases for various data
        types; not sure why though.
        >
        -MH
        >
        >

        Comment

        • William Stacey [MVP]

          #5
          Re: Difference between string and String?

          Split is an instance method, not a static member. So at least on 2.0, you
          can not do "string.Spl it" or "String.Spl it". You can with an string
          instance.

          --
          William Stacey [MVP]

          "clintonG" <csgallagher@RE MOVETHISTEXTmet romilwaukee.com wrote in message
          news:O6qIGXXzGH A.1268@TK2MSFTN GP02.phx.gbl...
          | They do seem to be different...
          | The Split method is not accessible when trying to use it as string.Split
          but
          | it is as String.Split
          | If they are the same how could that be?
          |
          |
          | <%= Clinton Gallagher
          | NET csgallagher AT metromilwaukee. com
          | URL http://clintongallagher.metromilwaukee.com/
          | MAP 43°2'17"N 88°2'37"W : 43°2'17"N 88°2'37"W
          |
          | "Michael" <mhugh@domain.c omwrote in message
          | news:%237ZMgiWz GHA.4968@TK2MSF TNGP05.phx.gbl. ..
          | *"Ronald S. Cook" <rcook@westinis .comwrote in message
          | news:eEr66ZWzGH A.4920@TK2MSFTN GP06.phx.gbl...
          | *I notice there are many terms in the .NET Framework like string and
          | String.
          | * What's the difference? Which should I use?
          | *
          | * I also see int and Int32. They are the same?
          | *
          | * Thanks,
          | * Ron
          | *
          | *
          | >
          | Ron,
          | >
          | They're the same.
          | String comes from the .NET class System.String and string (lower case)
          is
          | a C# alias for System.String.
          | I often see devs interchange the .NET and C# aliases for various data
          | types; not sure why though.
          | >
          | -MH
          | >
          | >
          |
          |


          Comment

          • Russell Mangel

            #6
            Re: Difference between string and String?

            Most of the code I have seen uses int for Int32,
            and string for String. But the choice is yours.

            In most cases using the alias types will be identical
            to the CLR type. However, value types like int
            (Int32), may not. Consider the following two loops:

            for (int i = 0; i < int.MaxValue; i++)
            {
            // Loop runs to Int32.MaxValue on 32-bit cpus.
            // Loop runs to Int64.MaxValue on 64-bit cpus.

            // The big difference here is this loop could have
            // more loops on 64-bit cpus.
            // Can your program tolerate this?
            }

            // Using CLR Types
            for (Int32 i = 0; i < Int32.MaxValue; i++)
            {
            // This loop will have the same number of iterations
            // on 32-bit or 64 bit cpus.
            }

            Personally I use the CLR types exclusively. So
            when I say Int32 i = 9; I know will will be 32bits
            and not 64 bits.

            Russell Mangel
            Las Vegas, NV


            Comment

            • Marc Gravell

              #7
              Re: Difference between string and String?

              No, according to the c# spec (3.4.2 and 4.1.4), int is System.Int32
              *always* (lessons learnt from the past...). IntPtr.Size changes to
              match the OS, however.

              Marc

              Comment

              • Jon Skeet [C# MVP]

                #8
                Re: Difference between string and String?

                Russell Mangel <russell@tymer. netwrote:
                Most of the code I have seen uses int for Int32,
                and string for String. But the choice is yours.
                >
                In most cases using the alias types will be identical
                to the CLR type. However, value types like int
                (Int32), may not. Consider the following two loops:
                >
                for (int i = 0; i < int.MaxValue; i++)
                {
                // Loop runs to Int32.MaxValue on 32-bit cpus.
                // Loop runs to Int64.MaxValue on 64-bit cpus.
                >
                // The big difference here is this loop could have
                // more loops on 64-bit cpus.
                // Can your program tolerate this?
                }
                Any evidence for this? I don't believe it's the case at all. int
                *always* means Int32 as far as I've ever seen in the standard.
                // Using CLR Types
                for (Int32 i = 0; i < Int32.MaxValue; i++)
                {
                // This loop will have the same number of iterations
                // on 32-bit or 64 bit cpus.
                }
                >
                Personally I use the CLR types exclusively. So
                when I say Int32 i = 9; I know will will be 32bits
                and not 64 bits.
                Would you feel differently if the above was proved not to be true? The
                spec is very clear on the matter. For example, from 11.1.5 (ECMA
                numbering for C# 2.0):

                <quote>
                The int type represents signed 32-bit integers with values from
                =3F2147483648 to 2147483647, inclusive.
                </quote>

                --
                Jon Skeet - <skeet@pobox.co m>
                http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
                If replying to the group, please do not mail me too

                Comment

                • mpetrotta@gmail.com

                  #9
                  Re: Difference between string and String?


                  Russell Mangel wrote:
                  Most of the code I have seen uses int for Int32,
                  and string for String. But the choice is yours.
                  >
                  In most cases using the alias types will be identical
                  to the CLR type. However, value types like int
                  (Int32), may not. Consider the following two loops:
                  >
                  for (int i = 0; i < int.MaxValue; i++)
                  {
                  // Loop runs to Int32.MaxValue on 32-bit cpus.
                  // Loop runs to Int64.MaxValue on 64-bit cpus.
                  >
                  // The big difference here is this loop could have
                  // more loops on 64-bit cpus.
                  // Can your program tolerate this?
                  }
                  >
                  // Using CLR Types
                  for (Int32 i = 0; i < Int32.MaxValue; i++)
                  {
                  // This loop will have the same number of iterations
                  // on 32-bit or 64 bit cpus.
                  }
                  Beg to differ. Running on a quad Xeon (EM64T) box, with Windows Server
                  2003 64-bit Enterprise Edition:

                  Console.WriteLi ne("int.MaxValu e = " + int.MaxValue);
                  Console.WriteLi ne("Int32.MaxVa lue = " + Int32.MaxValue) ;
                  Console.WriteLi ne("Int64.MaxVa lue = " + Int64.MaxValue) ;

                  int.MaxValue = 2147483647
                  Int32.MaxValue = 2147483647
                  Int64.MaxValue = 922337203685477 5807

                  And Jon's already pointed out the spec reference.

                  Michael

                  Comment

                  Working...