Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. String or string?

String or string?

Scheduled Pinned Locked Moved The Lounge
csharpdotnetquestion
62 Posts 17 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Lost User

    Now it gets confusing. The MSDN does not mention any of this at first glance for the String class. They use string in their samples when they want to use it as a value type and describe how initializing a string with a literal leads to the generation of code that uses one of String's constructors in MISL.

    The language is JavaScript. that of Mordor, which I will not utter here
    This is Javascript. If you put big wheels and a racing stripe on a golf cart, it's still a fucking golf cart.
    "I don't know, extraterrestrial?" "You mean like from space?" "No, from Canada." If software development were a circus, we would all be the clowns.

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #20

    Well it's built-in, so it's always going to be a bit odd. Still, you can tell it's not a value type by [cheating](http://ideone.com/v9xjcG), if it was actually a value type it should be impossible to change the original, but as this experiment shows it is only made "impossible" by the immutability.

    L 1 Reply Last reply
    0
    • L Lost User

      You mean like [this](http://ideone.com/zmqvAx) ?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #21

      I must admit that i have never tried to do that. Classes are reference types, (many) aliases are treated like value types. Always worked for me. Long ago I read a book when taking a look at .Net 1.0 and never had a need to question this.

      The language is JavaScript. that of Mordor, which I will not utter here
      This is Javascript. If you put big wheels and a racing stripe on a golf cart, it's still a fucking golf cart.
      "I don't know, extraterrestrial?" "You mean like from space?" "No, from Canada." If software development were a circus, we would all be the clowns.

      Richard DeemingR 1 Reply Last reply
      0
      • F Forogar

        When writing my C# code I was in the habit of using string (all lowercase) for strings declarations, etc. and String (capitalized) for method calls such as String.Empty and String.Format just as a sort of aide memoir that I was calling an object method. As I started to create String extension methods I reviewed this habit of mine and decided this was a pointless differentiation and switched to just using string all the time. At the same time I decided that my using Int32 for methods such as Int32.TryParse and just int in declarations, etc. was also pointless and perhaps confusing to others and so switched to using int all the time instead. It all compiles to the same IL code anyway so it was just a matter of style really. What do you think?

        - I would love to change the world, but they won’t give me the source code.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #22

        This thread is getting me extremely confused :confused: Just kill it, PLEASE :-O :laugh:

        Get me coffee and no one gets hurt!

        1 Reply Last reply
        0
        • L Lost User

          Quote:

          Could it be that you are confusing this with Java?

          No. I never studied Java. I probably was just confusing it with.....:confused:

          Get me coffee and no one gets hurt!

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #23

          .Net really never had primitive types, but down below I just heard that the line between the classes and the aliases as value types is not drawn as clearly as I believed.

          The language is JavaScript. that of Mordor, which I will not utter here
          This is Javascript. If you put big wheels and a racing stripe on a golf cart, it's still a fucking golf cart.
          "I don't know, extraterrestrial?" "You mean like from space?" "No, from Canada." If software development were a circus, we would all be the clowns.

          1 Reply Last reply
          0
          • I icemanind

            I've always used the lowercase string when declaring a type, but I use the uppercase String when calling static methods. For example:

            string myString = "Hello World!";
            string anotherString = String.Format("{0}", "Hola!");

            Microsoft seems to do the same thing, as seen in some of their examples, like here.

            B Offline
            B Offline
            Bryian Tan
            wrote on last edited by
            #24

            If the usage is correct, that make me think why the VS Intellisense still display "name can be simplified", "Show potential fixes", IDE0001 C# Name can be simplified. Isn't that "Show potential fixes" = there is a bug and here is the potential fix? After all VS is Microsoft product right? ;P ;P

            Bryian Tan

            T 1 Reply Last reply
            0
            • L Lost User

              Classes are reference types but Int32 is a struct

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #25

              That explains it. Then I'm right what String and string are concerned, but wrong about Int32 and int.

              The language is JavaScript. that of Mordor, which I will not utter here
              This is Javascript. If you put big wheels and a racing stripe on a golf cart, it's still a fucking golf cart.
              "I don't know, extraterrestrial?" "You mean like from space?" "No, from Canada." If software development were a circus, we would all be the clowns.

              Richard DeemingR 1 Reply Last reply
              0
              • L Lost User

                Well it's built-in, so it's always going to be a bit odd. Still, you can tell it's not a value type by [cheating](http://ideone.com/v9xjcG), if it was actually a value type it should be impossible to change the original, but as this experiment shows it is only made "impossible" by the immutability.

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #26

                I have just gotten the answer to this little riddle. Int32 is a struct, not a class, while String really is a class and string is just treated as a value type.

                The language is JavaScript. that of Mordor, which I will not utter here
                This is Javascript. If you put big wheels and a racing stripe on a golf cart, it's still a fucking golf cart.
                "I don't know, extraterrestrial?" "You mean like from space?" "No, from Canada." If software development were a circus, we would all be the clowns.

                J 1 Reply Last reply
                0
                • F Forogar

                  When writing my C# code I was in the habit of using string (all lowercase) for strings declarations, etc. and String (capitalized) for method calls such as String.Empty and String.Format just as a sort of aide memoir that I was calling an object method. As I started to create String extension methods I reviewed this habit of mine and decided this was a pointless differentiation and switched to just using string all the time. At the same time I decided that my using Int32 for methods such as Int32.TryParse and just int in declarations, etc. was also pointless and perhaps confusing to others and so switched to using int all the time instead. It all compiles to the same IL code anyway so it was just a matter of style really. What do you think?

                  - I would love to change the world, but they won’t give me the source code.

                  D Offline
                  D Offline
                  dandy72
                  wrote on last edited by
                  #27

                  Forogar wrote:

                  It all compiles to the same IL code anyway so it was just a matter of style really.

                  That's all that needs to be said, right?

                  Forogar wrote:

                  What do you think?

                  I think it's getting late on Friday afternoon and I'm already past the point where I can afford to waste the brain cells I'd need for something like this...

                  1 Reply Last reply
                  0
                  • L Lost User

                    I have just gotten the answer to this little riddle. Int32 is a struct, not a class, while String really is a class and string is just treated as a value type.

                    The language is JavaScript. that of Mordor, which I will not utter here
                    This is Javascript. If you put big wheels and a racing stripe on a golf cart, it's still a fucking golf cart.
                    "I don't know, extraterrestrial?" "You mean like from space?" "No, from Canada." If software development were a circus, we would all be the clowns.

                    J Offline
                    J Offline
                    Jon McKee
                    wrote on last edited by
                    #28

                    It's not even that it's being treated like a value type. The reason it can accept literals like that is because of an implicit conversion operator.

                    public class ImplicitString
                    {
                    public string Value { get; private set; }
                    public ImplicitString(string x)
                    {
                    Value = x;
                    }

                    public static implicit operator ImplicitString(string x)
                    {
                        return new ImplicitString(x);
                    }
                    

                    }

                    //Use
                    ImplicitString s = "Hello.";

                    1 Reply Last reply
                    0
                    • F Forogar

                      When writing my C# code I was in the habit of using string (all lowercase) for strings declarations, etc. and String (capitalized) for method calls such as String.Empty and String.Format just as a sort of aide memoir that I was calling an object method. As I started to create String extension methods I reviewed this habit of mine and decided this was a pointless differentiation and switched to just using string all the time. At the same time I decided that my using Int32 for methods such as Int32.TryParse and just int in declarations, etc. was also pointless and perhaps confusing to others and so switched to using int all the time instead. It all compiles to the same IL code anyway so it was just a matter of style really. What do you think?

                      - I would love to change the world, but they won’t give me the source code.

                      B Offline
                      B Offline
                      Bryian Tan
                      wrote on last edited by
                      #29

                      Even though we use the String, Int32, etc... correctly per Microsoft website, in VS, Intellisense will display suggestion "name can be simplified", "Show potential fixes", IDE0001 C# Name can be simplified. Isn't that "Show potential fixes" = there is a bug and here is the potential fix? Why Microsoft didn't fix it to clear this confusion? After all VS is Microsoft product right? Maybe there is a reason behind it, and we all going to like it ;P ;P ;P

                      Bryian Tan

                      J 1 Reply Last reply
                      0
                      • L Lost User

                        I must admit that i have never tried to do that. Classes are reference types, (many) aliases are treated like value types. Always worked for me. Long ago I read a book when taking a look at .Net 1.0 and never had a need to question this.

                        The language is JavaScript. that of Mordor, which I will not utter here
                        This is Javascript. If you put big wheels and a racing stripe on a golf cart, it's still a fucking golf cart.
                        "I don't know, extraterrestrial?" "You mean like from space?" "No, from Canada." If software development were a circus, we would all be the clowns.

                        Richard DeemingR Offline
                        Richard DeemingR Offline
                        Richard Deeming
                        wrote on last edited by
                        #30

                        CDP1802 wrote:

                        Classes are reference types, (many) aliases are treated like value types.

                        Nope. There is absolutely no difference between using the type name (Int32) and the alias (int). They are exactly the same thing. They compile to the same IL.

                        int x = new int();
                        Int32 y = 42;
                        Console.WriteLine($"{x}, {y}");
                        // Output: 0, 42

                        Java differentiates between the primitive types and their reference type equivalents. In .NET, there is no difference. It sounds like whoever wrote that book was confused, and managed to spread the confusion. :)


                        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                        L 1 Reply Last reply
                        0
                        • L Lost User

                          That explains it. Then I'm right what String and string are concerned, but wrong about Int32 and int.

                          The language is JavaScript. that of Mordor, which I will not utter here
                          This is Javascript. If you put big wheels and a racing stripe on a golf cart, it's still a fucking golf cart.
                          "I don't know, extraterrestrial?" "You mean like from space?" "No, from Canada." If software development were a circus, we would all be the clowns.

                          Richard DeemingR Offline
                          Richard DeemingR Offline
                          Richard Deeming
                          wrote on last edited by
                          #31

                          CDP1802 wrote:

                          Then I'm right what String and string are concerned

                          Nope again. :) String and string are exactly the same - an immutable reference type. The only reason you don't typically need to use new with a string is because the compiler has a deep knowledge of it. string.cs - Reference Source[^]


                          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                          L 1 Reply Last reply
                          0
                          • Richard DeemingR Richard Deeming

                            CDP1802 wrote:

                            Classes are reference types, (many) aliases are treated like value types.

                            Nope. There is absolutely no difference between using the type name (Int32) and the alias (int). They are exactly the same thing. They compile to the same IL.

                            int x = new int();
                            Int32 y = 42;
                            Console.WriteLine($"{x}, {y}");
                            // Output: 0, 42

                            Java differentiates between the primitive types and their reference type equivalents. In .NET, there is no difference. It sounds like whoever wrote that book was confused, and managed to spread the confusion. :)


                            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                            L Offline
                            L Offline
                            Lost User
                            wrote on last edited by
                            #32

                            We already got that sorted out, I think. Int32 is not a class, it's a struct. Now it makes sense and you are right. It makes no difference.

                            The language is JavaScript. that of Mordor, which I will not utter here
                            This is Javascript. If you put big wheels and a racing stripe on a golf cart, it's still a fucking golf cart.
                            "I don't know, extraterrestrial?" "You mean like from space?" "No, from Canada." If software development were a circus, we would all be the clowns.

                            P L 2 Replies Last reply
                            0
                            • Richard DeemingR Richard Deeming

                              CDP1802 wrote:

                              Then I'm right what String and string are concerned

                              Nope again. :) String and string are exactly the same - an immutable reference type. The only reason you don't typically need to use new with a string is because the compiler has a deep knowledge of it. string.cs - Reference Source[^]


                              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                              L Offline
                              L Offline
                              Lost User
                              wrote on last edited by
                              #33

                              Drat! :-)

                              The language is JavaScript. that of Mordor, which I will not utter here
                              This is Javascript. If you put big wheels and a racing stripe on a golf cart, it's still a fucking golf cart.
                              "I don't know, extraterrestrial?" "You mean like from space?" "No, from Canada." If software development were a circus, we would all be the clowns.

                              1 Reply Last reply
                              0
                              • F Forogar

                                When writing my C# code I was in the habit of using string (all lowercase) for strings declarations, etc. and String (capitalized) for method calls such as String.Empty and String.Format just as a sort of aide memoir that I was calling an object method. As I started to create String extension methods I reviewed this habit of mine and decided this was a pointless differentiation and switched to just using string all the time. At the same time I decided that my using Int32 for methods such as Int32.TryParse and just int in declarations, etc. was also pointless and perhaps confusing to others and so switched to using int all the time instead. It all compiles to the same IL code anyway so it was just a matter of style really. What do you think?

                                - I would love to change the world, but they won’t give me the source code.

                                Sander RosselS Offline
                                Sander RosselS Offline
                                Sander Rossel
                                wrote on last edited by
                                #34

                                I used to do the same, string variable and String.Format(...) and int variable and Int32.Parse(...). There was a good reason I did that though, Microsoft recommended doing it! Until I switched to Visual Studio 2015 and all of a sudden it started giving me "tips" to simplify String to string and Int32 to int... Thanks Microsoft, for sticking to your own guidelines :~ For the same reason I stopped using this and base (unless necessary) and TheClassImIn.StaticMethod instead of simply StaticMethod. And yes, I know I can turn off those rules, but I like sticking to defaults :)

                                Best, Sander arrgh.js - Bringing LINQ to JavaScript SQL Server for C# Developers Succinctly Object-Oriented Programming in C# Succinctly

                                L 1 Reply Last reply
                                0
                                • L Lost User

                                  There is a tiny difference, remember? Classes are reference types while the aliases are made to appear like value types for convenience.

                                  The language is JavaScript. that of Mordor, which I will not utter here
                                  This is Javascript. If you put big wheels and a racing stripe on a golf cart, it's still a fucking golf cart.
                                  "I don't know, extraterrestrial?" "You mean like from space?" "No, from Canada." If software development were a circus, we would all be the clowns.

                                  P Offline
                                  P Offline
                                  PIEBALDconsult
                                  wrote on last edited by
                                  #35

                                  No, there is no difference.

                                  L 1 Reply Last reply
                                  0
                                  • L Lost User

                                    We already got that sorted out, I think. Int32 is not a class, it's a struct. Now it makes sense and you are right. It makes no difference.

                                    The language is JavaScript. that of Mordor, which I will not utter here
                                    This is Javascript. If you put big wheels and a racing stripe on a golf cart, it's still a fucking golf cart.
                                    "I don't know, extraterrestrial?" "You mean like from space?" "No, from Canada." If software development were a circus, we would all be the clowns.

                                    P Offline
                                    P Offline
                                    PIEBALDconsult
                                    wrote on last edited by
                                    #36

                                    structs are also classes... (PIEBALD exits quickly...)

                                    L 1 Reply Last reply
                                    0
                                    • P PIEBALDconsult

                                      No, there is no difference.

                                      L Offline
                                      L Offline
                                      Lost User
                                      wrote on last edited by
                                      #37

                                      Ok, then it's time to do some reading.

                                      The language is JavaScript. that of Mordor, which I will not utter here
                                      This is Javascript. If you put big wheels and a racing stripe on a golf cart, it's still a fucking golf cart.
                                      "I don't know, extraterrestrial?" "You mean like from space?" "No, from Canada." If software development were a circus, we would all be the clowns.

                                      1 Reply Last reply
                                      0
                                      • Richard DeemingR Richard Deeming

                                        PIEBALDconsult wrote:

                                        What I find crazy is that when specifying the underlying type for an enumeration, you must use the alias. X|

                                        Not if you're using the Roslyn (C# 6 / VS2015) compiler:

                                        enum Foo : Int32
                                        {
                                        Bar,
                                        Baz,
                                        }


                                        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                                        P Offline
                                        P Offline
                                        PIEBALDconsult
                                        wrote on last edited by
                                        #38

                                        No thanks. I prefer to write backward-compatible code unless there's a real reason to do otherwise.

                                        1 Reply Last reply
                                        0
                                        • P PIEBALDconsult

                                          structs are also classes... (PIEBALD exits quickly...)

                                          L Offline
                                          L Offline
                                          Lost User
                                          wrote on last edited by
                                          #39

                                          I know, but they are not treated as reference types and ... I have a better idea. here its past 11 PM now. Do you have something to drink?

                                          The language is JavaScript. that of Mordor, which I will not utter here
                                          This is Javascript. If you put big wheels and a racing stripe on a golf cart, it's still a fucking golf cart.
                                          "I don't know, extraterrestrial?" "You mean like from space?" "No, from Canada." If software development were a circus, we would all be the clowns.

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups