9

I just noticed on string they have a lot of extension methods that I guess I never noticed on strings.

Some are like

IsEmpty() // Seems to be equivalent to  String.IsNullOrEmpty() 
AsInt() //  seems to be equivalent to Convert.ToInt32(string); - does it throw exception as well?

I am just wonder do they use the same code under the hook and these are just away to reduce typing or is more going on?

Some do seem to be missing though like

 String.IsNullOrWhiteSpace()

Edit

Sorry when I said String.IsNullOrWhiteSpace() is missing I ment that there was no extension method. I do have that method is I write what I do above.

It also seems that these are not standard in the framework so I am trying to figure out where they came from?

I am not sure if resharper added these or if some other reference I have. I don't think I ever imported any extension plugin.

When I click definition over IsEmpty()

I get this

#region Assembly System.Web.WebPages.dll, v4.0.30319
// c:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Pages\v1.0\Assemblies\System.Web.WebPages.dll
#endregion

using System;
using System.Runtime.CompilerServices;

namespace System.Web.WebPages
{
    // Summary:
    //     Provides utility methods for converting string values to other data types.
    public static class StringExtensions
    {
        // Summary:
        //     Converts a string to a strongly typed value of the specified data type.
        //
        // Parameters:
        //   value:
        //     The value to convert.
        //
        // Type parameters:
        //   TValue:
        //     The data type to convert to.
        //
        // Returns:
        //     The converted value.
        public static TValue As<TValue>(this string value);
        //
        // Summary:
        //     Converts a string to the specified data type and specifies a default value.
        //
        // Parameters:
        //   value:
        //     The value to convert.
        //
        //   defaultValue:
        //     The value to return if value is null.
        //
        // Type parameters:
        //   TValue:
        //     The data type to convert to.
        //
        // Returns:
        //     The converted value.
        public static TValue As<TValue>(this string value, TValue defaultValue);
        //
        // Summary:
        //     Converts a string to a Boolean (true/false) value.
        //
        // Parameters:
        //   value:
        //     The value to convert.
        //
        // Returns:
        //     The converted value.
        public static bool AsBool(this string value);
        //
        // Summary:
        //     Converts a string to a Boolean (true/false) value and specifies a default
        //     value.
        //
        // Parameters:
        //   value:
        //     The value to convert.
        //
        //   defaultValue:
        //     The value to return if value is null or an invalid value. The default is
        //     false.
        //
        // Returns:
        //     The converted value.
        public static bool AsBool(this string value, bool defaultValue);
        //
        // Summary:
        //     Converts a string to a System.DateTime value.
        //
        // Parameters:
        //   value:
        //     The value to convert.
        //
        // Returns:
        //     The converted value.
        public static DateTime AsDateTime(this string value);
        //
        // Summary:
        //     Converts a string to a System.DateTime value and specifies a default value.
        //
        // Parameters:
        //   value:
        //     The value to convert.
        //
        //   defaultValue:
        //     The value to return if value is null or an invalid value. The default is
        //     the minimum time value on the system.
        //
        // Returns:
        //     The converted value.
        public static DateTime AsDateTime(this string value, DateTime defaultValue);
        //
        // Summary:
        //     Converts a string to a System.Decimal number.
        //
        // Parameters:
        //   value:
        //     The value to convert.
        //
        // Returns:
        //     The converted value.
        public static decimal AsDecimal(this string value);
        //
        // Summary:
        //     Converts a string to a System.Decimal number and specifies a default value.
        //
        // Parameters:
        //   value:
        //     The value to convert.
        //
        //   defaultValue:
        //     The value to return if value is null or invalid.
        //
        // Returns:
        //     The converted value.
        public static decimal AsDecimal(this string value, decimal defaultValue);
        //
        // Summary:
        //     Converts a string to a System.Single number.
        //
        // Parameters:
        //   value:
        //     The value to convert.
        //
        // Returns:
        //     The converted value.
        public static float AsFloat(this string value);
        //
        // Summary:
        //     Converts a string to a System.Single number and specifies a default value.
        //
        // Parameters:
        //   value:
        //     The value to convert.
        //
        //   defaultValue:
        //     The value to return if value is null.
        //
        // Returns:
        //     The converted value.
        public static float AsFloat(this string value, float defaultValue);
        //
        // Summary:
        //     Converts a string to an integer.
        //
        // Parameters:
        //   value:
        //     The value to convert.
        //
        // Returns:
        //     The converted value.
        public static int AsInt(this string value);
        //
        // Summary:
        //     Converts a string to an integer and specifies a default value.
        //
        // Parameters:
        //   value:
        //     The value to convert.
        //
        //   defaultValue:
        //     The value to return if value is null or is an invalid value.
        //
        // Returns:
        //     The converted value.
        public static int AsInt(this string value, int defaultValue);
        //
        // Summary:
        //     Checks whether a string can be converted to the specified data type.
        //
        // Parameters:
        //   value:
        //     The value to test.
        //
        // Type parameters:
        //   TValue:
        //     The data type to convert to.
        //
        // Returns:
        //     true if value can be converted to the specified type; otherwise, false.
        public static bool Is<TValue>(this string value);
        //
        // Summary:
        //     Checks whether a string can be converted to the Boolean (true/false) type.
        //
        // Parameters:
        //   value:
        //     The string value to test.
        //
        // Returns:
        //     true if value can be converted to the specified type; otherwise, false.
        public static bool IsBool(this string value);
        //
        // Summary:
        //     Checks whether a string can be converted to the System.DateTime type.
        //
        // Parameters:
        //   value:
        //     The string value to test.
        //
        // Returns:
        //     true if value can be converted to the specified type; otherwise, false.
        public static bool IsDateTime(this string value);
        //
        // Summary:
        //     Checks whether a string can be converted to the System.Decimal type.
        //
        // Parameters:
        //   value:
        //     The string value to test.
        //
        // Returns:
        //     true if value can be converted to the specified type; otherwise, false.
        public static bool IsDecimal(this string value);
        //
        // Summary:
        //     Checks whether a string value is null or empty.
        //
        // Parameters:
        //   value:
        //     The string value to test.
        //
        // Returns:
        //     true if value is null or is a zero-length string (""); otherwise, false.
        public static bool IsEmpty(this string value);
        //
        // Summary:
        //     Checks whether a string can be converted to the System.Single type.
        //
        // Parameters:
        //   value:
        //     The string value to test.
        //
        // Returns:
        //     true if value can be converted to the specified type; otherwise, false.
        public static bool IsFloat(this string value);
        //
        // Summary:
        //     Checks whether a string can be converted to an integer.
        //
        // Parameters:
        //   value:
        //     The string value to test.
        //
        // Returns:
        //     true if value can be converted to the specified type; otherwise, false.
        public static bool IsInt(this string value);
    }
}
chobo2
  • 83,322
  • 195
  • 530
  • 832
  • 2
    `String.IsNullOrWhiteSpace()` is new in 4.0, so depending on your version you may expect it not to be there (unless, of course, "they" have added it) – Matthew Feb 14 '11 at 17:45
  • 1
    Put cursor on `IsEmpty()` method call. Hit F12. See where it takes you. – Kent Boogaart Feb 14 '11 at 17:48

4 Answers4

15

These aren't "standard" extension methods - chances are they've been added by someone else working on your project. That means we can't tell what the code's doing under the hood, but you should be able to find out yourself.

In Visual Studio you should be able to navigate to the definition of either method - it will either show which assembly the method is in, or go straight to the source code if it can.

EDIT: Given the comments, it looks like they're the extension methods from MVC's StringExtensions class... which violates various bad practices in naming, as far as I can tell - particularly using language specific names within method names instead of the CLR type name. (So AsFloat should be AsSingle for example.) I'd also argue that it should be "To" rather than "As" given that it's providing a full conversion rather than just returning a view on the original value. Bah humbug 'n all.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • @Jon Skeet- I don't think someone else on my project made these as the only other person working on this project is mostly doing the client side and has no idea how to make an extension method. Does resharper make these? I am not sure how to find out who made them. I edited what I see when I go to definition over one. – chobo2 Feb 14 '11 at 17:45
  • 1
    @chobo, then they could have been added in a DLL you may be referencing. In your code where one of these methods appear, right click and select "go to definition" (or hit F12) to see if you can arrive at the source. (If it's external, you'll just see metadata, not the actual implementation.) – Anthony Pegram Feb 14 '11 at 17:47
  • @chobo2: No, ReSharper isn't going to be adding extension methods to your solution for you. As Anthony says in the comments and I say in my answer, navigate to the definition and you'll find out where they're coming from. One sure hint that they're *not* from Microsoft is that "AsInt" is a bad name for the conversion method. It would be ToInt32, or something similar. – Jon Skeet Feb 14 '11 at 17:48
  • @Jon Skeet - Ok I did that and it seems to be coming from System.Web.WebPages. Is that not a standard library? – chobo2 Feb 14 '11 at 17:50
  • @chobo2: It's not one that I'm familiar with, certainly. A quick search around shows it being part of MVC though... but `AsInt` is a *terrible* method name. I'm really surprised it got through. See http://msdn.microsoft.com/en-us/library/system.web.webpages.stringextensions(v=vs.99).aspx for more. (Note that MVC isn't part of the standard framework...) I've edited my answer accordingly. – Jon Skeet Feb 14 '11 at 17:52
  • @Jon Skeet - Ya that is true. I guess it is now part of the standard asp.net mvc 3 framework. I am not sure if I should use these extensions or just continue using what I been using. – chobo2 Feb 14 '11 at 17:55
  • @chobo2: Well I have reasonable hope that the implementation is better than the naming. Use whichever gives you the most readable code... just remember that these *do* come from MVC, so don't try using them from WPF etc :) – Jon Skeet Feb 14 '11 at 17:56
  • @Jon Skeet - Ya I hope so too. I think I will not use them just for the reason of keeping my code consistent. I don't want to import this .dll(that is called .web) into my service layer or repository that are in different projects as I am trying to keep these layers as free of these kind of dependencies as possible. I don't reference any mvc stuff in my service layer and I want to keep it that way as it should not know about it. To me it does not seem worth it breaking this to gain so little. I mean I could right my own extension if I need to(with better names lol). – chobo2 Feb 14 '11 at 18:01
  • when i use `IsEmpty` it says: `'string' does not contain a definition for IsEmpty` , can i use `IsEmpty` in [msdn](https://msdn.microsoft.com/en-us/library/system.web.webpages.stringextensions.isempty%28v=vs.99%29.aspx) or should i use `IsNullOrEmpty` ? – Shaiju T Nov 03 '15 at 14:50
  • 1
    @stom: Well it sounds like you're missing either an assembly reference or a using directive. I can't really tell just from that comment. – Jon Skeet Nov 03 '15 at 16:34
  • you are absolutely correct, i didnt add `using System.Web.WebPages;` my assembly version is 2.0.0.0, i tried before to right click and refract and now tried keyboards shortcuts mentioned [here](http://stackoverflow.com/questions/148977/visual-studio-keyboard-shortcut-to-automatically-add-the-needed-using-statement), but visual studio doesn't provide option to add using directive automatically, so i did it manually. Thank you. – Shaiju T Nov 04 '15 at 06:46
2

Some do seem to be missing though like

String.IsNullOrWhiteSpace()

That one was introduced in Fx4, are you running 3.5 ?

H H
  • 263,252
  • 30
  • 330
  • 514
1

I use for long time now this Extension method for every string...it's easy to use it, quick....make extension methods your daily habit where you can and should and you will see big difference writing code.

Create a library and use it across your applications....

public static bool IsNullOrEmpty(this string testString)
        {
            return string.IsNullOrEmpty(testString);
        }
George Taskos
  • 8,324
  • 18
  • 82
  • 147
0

Check your references - the extension methods can be defined in one of the libraries you've referenced. You can try removing one reference at a time.

Jakub Konecki
  • 45,581
  • 7
  • 87
  • 126