Like Tree1Likes
  • 1 Post By VirtualAce

string literals in C#

This is a discussion on string literals in C# within the C# Programming forums, part of the General Programming Boards category; I have a huge xml file from which the key and value attributes are selected among other things. Code: foreach ...

  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,164
    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
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Might I suggest using serialization for the xml file as opposed to parsing it manually through the DOM?
    rags_to_riches likes this.

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, 10:41 AM
  2. Can't add string literals.
    By computerquip in forum C++ Programming
    Replies: 3
    Last Post: 11-13-2008, 12: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, 01:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21