Thread: string literals in C#

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    330

    string literals in C#

    I have a huge xml file from which the key and value attributes are selected among other things.

    Code:
    foreach (
    XmlNode node in 
    configProductCode.SelectNodes("/configuration/appSettings/add"))
    {
      ConfigProductCode cpc = new ConfigProductCode();
    
      XmlAttribute keyAttr = node.Attributes["key"];
      XmlAttribute valAttr = node.Attributes["value"];
    // etc
    }
    How does C# handle the instantiation of literal strings? does it make a new object with allocating heap memory on every iteration or just once?

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    My understanding is that string literals aren't instantiated. They're just referenced from the assembly. Your method shouldn't incur any runtime cost.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Might I suggest using serialization for the xml file as opposed to parsing it manually through the DOM?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help! string literals
    By cakestler in forum C Programming
    Replies: 16
    Last Post: 02-05-2009, 11:41 AM
  2. Can't add string literals.
    By computerquip in forum C++ Programming
    Replies: 3
    Last Post: 11-13-2008, 01:20 AM
  3. UTF-8 string literals
    By pdc in forum C Programming
    Replies: 6
    Last Post: 07-28-2005, 02:52 PM
  4. String Literals
    By Trauts in forum C++ Programming
    Replies: 8
    Last Post: 05-17-2003, 08:39 PM
  5. string literals vs c-strings
    By axon in forum C++ Programming
    Replies: 13
    Last Post: 04-05-2003, 02:49 PM