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. Confused with string reference type

Confused with string reference type

Scheduled Pinned Locked Moved C#
question
7 Posts 6 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.
  • R Offline
    R Offline
    Rahul83
    wrote on last edited by
    #1

    Hi friends.. string str="ABC"; this means that we are assigning value to the object str of string class..right?But whn we are instantiating some class then we are assigning values to the feilds of that class(ex:Employee_1.Name="xyz",Employee_1.Id=1234,etc where Employee_1 is an object of class Employee)..thn how things are working differently for string class..I think this has to do something with user defined and predefined reference data types..

    C D M G 4 Replies Last reply
    0
    • R Rahul83

      Hi friends.. string str="ABC"; this means that we are assigning value to the object str of string class..right?But whn we are instantiating some class then we are assigning values to the feilds of that class(ex:Employee_1.Name="xyz",Employee_1.Id=1234,etc where Employee_1 is an object of class Employee)..thn how things are working differently for string class..I think this has to do something with user defined and predefined reference data types..

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      All reference types are the same, regardless if the type is user defined or "predefined".


      Upcoming events: * Glasgow: SQL Server 2005 - XML and XML Query Plans, Mock Objects, SQL Server Reporting Services... Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton My website

      1 Reply Last reply
      0
      • R Rahul83

        Hi friends.. string str="ABC"; this means that we are assigning value to the object str of string class..right?But whn we are instantiating some class then we are assigning values to the feilds of that class(ex:Employee_1.Name="xyz",Employee_1.Id=1234,etc where Employee_1 is an object of class Employee)..thn how things are working differently for string class..I think this has to do something with user defined and predefined reference data types..

        D Offline
        D Offline
        DavidNohejl
        wrote on last edited by
        #3

        Literal "ABC" IS instance of type string.


        "Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe

        1 Reply Last reply
        0
        • R Rahul83

          Hi friends.. string str="ABC"; this means that we are assigning value to the object str of string class..right?But whn we are instantiating some class then we are assigning values to the feilds of that class(ex:Employee_1.Name="xyz",Employee_1.Id=1234,etc where Employee_1 is an object of class Employee)..thn how things are working differently for string class..I think this has to do something with user defined and predefined reference data types..

          M Offline
          M Offline
          Manoj Kumar Rai
          wrote on last edited by
          #4

          Hi, Even your question is not very clear:-) Let me explain you the difference between the predefined and user defined data types. See if "AA" is a user defined class, while creating an instance of the classs AA, "new" operator must be used. i.e. AA a = new AA(); Only, after this we can set/get the non-static members. In case of predfined types like "string", the C# compiler knows of them advance, hence uses "new" operator internally. i.e. In case of string str = "ABC"; C# compiler knows about string class, hence does str = new string("ABC") internally. Manoj

          S R 2 Replies Last reply
          0
          • M Manoj Kumar Rai

            Hi, Even your question is not very clear:-) Let me explain you the difference between the predefined and user defined data types. See if "AA" is a user defined class, while creating an instance of the classs AA, "new" operator must be used. i.e. AA a = new AA(); Only, after this we can set/get the non-static members. In case of predfined types like "string", the C# compiler knows of them advance, hence uses "new" operator internally. i.e. In case of string str = "ABC"; C# compiler knows about string class, hence does str = new string("ABC") internally. Manoj

            S Offline
            S Offline
            Sandeep Akhare
            wrote on last edited by
            #5

            Thats nice explaination Keep it up :) :):)

            Thanks and Regards Sandeep If If you look at what you do not have in life, you don't have anything, If you look at what you have in life, you have everything... "

            1 Reply Last reply
            0
            • M Manoj Kumar Rai

              Hi, Even your question is not very clear:-) Let me explain you the difference between the predefined and user defined data types. See if "AA" is a user defined class, while creating an instance of the classs AA, "new" operator must be used. i.e. AA a = new AA(); Only, after this we can set/get the non-static members. In case of predfined types like "string", the C# compiler knows of them advance, hence uses "new" operator internally. i.e. In case of string str = "ABC"; C# compiler knows about string class, hence does str = new string("ABC") internally. Manoj

              R Offline
              R Offline
              Rahul83
              wrote on last edited by
              #6

              Thanks for such short and sweet answer..nw i understood.

              1 Reply Last reply
              0
              • R Rahul83

                Hi friends.. string str="ABC"; this means that we are assigning value to the object str of string class..right?But whn we are instantiating some class then we are assigning values to the feilds of that class(ex:Employee_1.Name="xyz",Employee_1.Id=1234,etc where Employee_1 is an object of class Employee)..thn how things are working differently for string class..I think this has to do something with user defined and predefined reference data types..

                G Offline
                G Offline
                Guffa
                wrote on last edited by
                #7

                The only special about strings, is that you can write string literals in the code. When the code is compiled, the string literals are created as constant string objects. So, this code: string str = "ABC"; works more like this: const string strconst001 = "ABC"; // created at compile time string str = strconst001; // just assign the reference So, when you assign a string literal, you don't really create a string object, you only assign the reference of a string object that already exists. When you actually create a new string object, it works like any other class. Example: string str = new String('*', 42);

                --- single minded; short sighted; long gone;

                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