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. General Programming
  3. C#
  4. string versus String !

string versus String !

Scheduled Pinned Locked Moved C#
questiontutorial
18 Posts 9 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.
  • S Saksida Bojan

    They are the same. If you want to use reference try like this:

    String someString = "";
    DoSomething(ref someString);

    // Now someString has new value

    Function:
    private void DoSomething(ref String str)
    {
    str = "Was Empty";
    }

    H Offline
    H Offline
    humayunlalzad
    wrote on last edited by
    #5

    Why is the ref keyword added to String in this example. Isn't string a reference type already? I thought ref is only to make value types behave like reference types.

    S L 2 Replies Last reply
    0
    • S Scott Dorman

      There is no difference. string is the C# alias to String (which is really System.String). When the code compiles, all of your string types are converted to System.String in the IL. Personally, I prefer using string when I am typing a variable and String when I am acessing static members of the class. This is only personal preference as I think it makes the code easier to follow. There is nothing wrong with code like:

      bool empty = string.IsNullOrEmpty("");

      instead of

      bool empty = String.IsNullOrEmpty("");

      Scott Dorman

      Microsoft® MVP - Visual C# | MCPD President - Tampa Bay IASA [Blog][Articles][Forum Guidelines]


      Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai

      _ Offline
      _ Offline
      _Q12_
      wrote on last edited by
      #6

      thanks....now I see. :-O

      S 1 Reply Last reply
      0
      • _ _Q12_

        thanks....now I see. :-O

        S Offline
        S Offline
        Scott Dorman
        wrote on last edited by
        #7

        Glad to help.

        Scott Dorman

        Microsoft® MVP - Visual C# | MCPD President - Tampa Bay IASA [Blog][Articles][Forum Guidelines]


        Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai

        1 Reply Last reply
        0
        • _ _Q12_

          string versus String ! I have a question (a little embarrassing because I see it so late), what is the difference between String and string ? not in the way that String is a Class and string is a reference type. But rather how to think about them in the term of uses. I searched and find this piece of gold, but I don't know how to use it very well: Use "String" to refer specifically to the String class. Use "string" when referring to an object of the String class. How to use them, accordingly? thanks again.

          _ Offline
          _ Offline
          _Q12_
          wrote on last edited by
          #8

          look over this file and tell me how are you thinking in improve it ! I spend an entire night at it. The idea is to have a MAP (on the paper) and rapidly look over it and "remember" what i must to put when I need it. it's like a pocket reminder(a very tiny little map). http://www.sendspace.com/file/p56n1r of course in time must be the answer....if any...hope so... :) thanks again.

          1 Reply Last reply
          0
          • S Scott Dorman

            There is no difference. string is the C# alias to String (which is really System.String). When the code compiles, all of your string types are converted to System.String in the IL. Personally, I prefer using string when I am typing a variable and String when I am acessing static members of the class. This is only personal preference as I think it makes the code easier to follow. There is nothing wrong with code like:

            bool empty = string.IsNullOrEmpty("");

            instead of

            bool empty = String.IsNullOrEmpty("");

            Scott Dorman

            Microsoft® MVP - Visual C# | MCPD President - Tampa Bay IASA [Blog][Articles][Forum Guidelines]


            Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai

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

            Scott Dorman wrote:

            I prefer using string when I am typing a variable and String when I am acessing static members of the class.

            Me too, except I type System.String.

            L 2 Replies Last reply
            0
            • _ _Q12_

              string versus String ! I have a question (a little embarrassing because I see it so late), what is the difference between String and string ? not in the way that String is a Class and string is a reference type. But rather how to think about them in the term of uses. I searched and find this piece of gold, but I don't know how to use it very well: Use "String" to refer specifically to the String class. Use "string" when referring to an object of the String class. How to use them, accordingly? thanks again.

              M Offline
              M Offline
              murktinez
              wrote on last edited by
              #10

              I'm pretty sure the only reason they have a the String class is because Java has a String class.

              1 Reply Last reply
              0
              • H humayunlalzad

                Why is the ref keyword added to String in this example. Isn't string a reference type already? I thought ref is only to make value types behave like reference types.

                S Offline
                S Offline
                Saksida Bojan
                wrote on last edited by
                #11

                humayunlalzad wrote:

                Why is the ref keyword added to String in this example. Isn't string a reference type already? I thought ref is only to make value types behave like reference types.

                I realy don't know about string, but i must have put into where i have struct type.

                1 Reply Last reply
                0
                • H humayunlalzad

                  Why is the ref keyword added to String in this example. Isn't string a reference type already? I thought ref is only to make value types behave like reference types.

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #12

                  Hi, I suggest you run something similar to this:

                  string str="aha";
                  log(str);
                  func1(str);
                  log(str);
                  func2(ref str);
                  log(str);

                  private void func1(String str) {str = "Changed"; log("1 "+str);}
                  private void func2(ref String str) {str = "Changed";log("2 "+str); }
                  private void log(string s) {Console.WriteLine(s);}

                  and think about why it behaves the way it does. :)

                  Luc Pattyn [Forum Guidelines] [My Articles]


                  The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


                  1 Reply Last reply
                  0
                  • P PIEBALDconsult

                    Scott Dorman wrote:

                    I prefer using string when I am typing a variable and String when I am acessing static members of the class.

                    Me too, except I type System.String.

                    L Offline
                    L Offline
                    Luc Pattyn
                    wrote on last edited by
                    #13

                    as System.Boolean.True hasn't been provided, do you also type System.Boolean.Parse(System.Boolean.TrueString) instead of true? :-D

                    Luc Pattyn [Forum Guidelines] [My Articles]


                    The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


                    P 1 Reply Last reply
                    0
                    • L Luc Pattyn

                      as System.Boolean.True hasn't been provided, do you also type System.Boolean.Parse(System.Boolean.TrueString) instead of true? :-D

                      Luc Pattyn [Forum Guidelines] [My Articles]


                      The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


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

                      Don't tempt me; every once in a while I think about not using any of the aliases at all (they're all just syntactic sugar after all). Unfortunately they're required to specify base types for Enumerations :mad: .

                      1 Reply Last reply
                      0
                      • P PIEBALDconsult

                        Scott Dorman wrote:

                        I prefer using string when I am typing a variable and String when I am acessing static members of the class.

                        Me too, except I type System.String.

                        L Offline
                        L Offline
                        Luc Pattyn
                        wrote on last edited by
                        #15

                        I have great news for you: System.String System.String System.String The second one has "lang=none" in the CODE tag. :)

                        Luc Pattyn [Forum Guidelines] [My Articles]


                        The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


                        L P 2 Replies Last reply
                        0
                        • L Luc Pattyn

                          I have great news for you: System.String System.String System.String The second one has "lang=none" in the CODE tag. :)

                          Luc Pattyn [Forum Guidelines] [My Articles]


                          The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


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

                          Cool, is stuff like that documented somewhere? (am I just bad at searching?)

                          L 1 Reply Last reply
                          0
                          • L Lost User

                            Cool, is stuff like that documented somewhere? (am I just bad at searching?)

                            L Offline
                            L Offline
                            Luc Pattyn
                            wrote on last edited by
                            #17

                            I trust it is, however for harmless things like that, I just experiment a bit. I always allow for a few minutes of heuristics before taking the scientific route. :)

                            Luc Pattyn [Forum Guidelines] [My Articles]


                            The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


                            1 Reply Last reply
                            0
                            • L Luc Pattyn

                              I have great news for you: System.String System.String System.String The second one has "lang=none" in the CODE tag. :)

                              Luc Pattyn [Forum Guidelines] [My Articles]


                              The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


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

                              Yes, Chris enlightened me about that a day or two ago. I still say that text should be the default.

                              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