Thread: a couple of questions about System.String

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445

    a couple of questions about System.String

    System.String lacks a couple of things that I would have thought would be standard behavior. First, it has no member to tell you if the string is empty. the only way is to write the code

    Code:
    if (myString.length == 0)
    The C++ std::string has this functionality, and I use it all the time.

    Second, I cannot derive a class from System.String to add this functionality. Why would microsoft make it a sealed class, when it's reasonable to expect that someone might want to inherit the functionality of a string?

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    .NET 2.0 has the static member String.IsNullOrEmpty(myString).

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    In recent versions of dot net (3.0+ ?) there are extension methods. Technically they are static functions but act as if they were methods. LINQ makes good use of these.

    If you want the method approach you could write one like this (note the "this" in the argument list):

    Code:
    namespace StringExtensions
    {
    	public static class StringExtensions
    	{
    		public static bool IsEmpty(this string String)
    		{
    			return string.IsNullOrEmpty(String);
    		}
    	}
    }
    And then be able to use it like this:

    Code:
    using StringExtensions;
    
    var MyString = "Hello";
    MyString.IsEmpty();
    The namespace must be opened manually ("using") or the extension methods cannot be found.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Unfortunately, I do not know where or when this was introduced, but it has a member called Empty.

    Code:
    if(someString == string.Empty)
        // .. etc ..
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by IceDane View Post
    Unfortunately, I do not know where or when this was introduced, but it has a member called Empty.

    Code:
    if(someString == string.Empty)
        // .. etc ..
    I noticed that too, but it's not quite what i was looking for. I studied up on extension methods, and that got me exactly what I needed.

    Thanks everyone.

  6. #6
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Quote Originally Posted by Elkvis View Post
    I noticed that too, but it's not quite what i was looking for. I studied up on extension methods, and that got me exactly what I needed.

    Thanks everyone.
    Extension methods are really awesome indeed, but keep in mind that they are a relatively new addition to the framework. They were only added as late as C# 3.0.
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Couple of simple directdraw questions.
    By Deo in forum Game Programming
    Replies: 3
    Last Post: 05-25-2005, 07:55 AM
  2. A couple of questions that someone might know the answer to...
    By Finchie_88 in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 04-15-2005, 08:26 AM
  3. Studying for Final, Couple Questions...
    By stewade in forum C Programming
    Replies: 4
    Last Post: 05-10-2004, 05:02 PM
  4. A couple of OOP questions
    By codec in forum C++ Programming
    Replies: 5
    Last Post: 05-03-2004, 07:18 PM
  5. A couple of Questions
    By johnnabn in forum C++ Programming
    Replies: 4
    Last Post: 02-24-2003, 10:10 PM