c# question

  • Why we don't instantiate the string class in c#?

    Any answers will be appreciated

  • To change the actual contents of the string, suppose, we can use

    System.Text.StringBuilder.

     

    Other members can chip in.....

    Ram

  • Strings in C# (and Java) are immutable. That is, once created, they cannot be changed. Usually, when you instantiate a class, you create a default object with the intent of modifying its state later. When you create a string, the value is not expected to change during the execution of the scope in which it is defined. There are, of course, many times we do want to change the contents.

      StringVar = "some data";
      ...
      StringVar = "other data";

    What is happening is the pointer "StringVar" is adjusted to refer to another string value elsewhere in memory. The old string value may be marked for garbage collection and the space reused.

    When you want a string that you know will be changed a lot (for example, you build up a dynamic SQL statement from info you gather), then both C# and Java provide more flexible string types such as StringBuffer.

    If you want to go into the particulars (advantages, purposes of, how to define) immutable data types, just about any intro book for either language will help.

    Tomm Carr
    --
    Version Normal Form -- http://groups.google.com/group/vrdbms

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply