To make this simple, we'll create a new console application, add a class and name it Extensions or any name you want, I usually stick with some naming conventions so Extensions is pretty descriptive.
Now, we need to make that class public and static for this to work. The code is very simple..see below:
Notice that we used the keyword "This" to let it know that this is going to be an extended method.public static class Extensions{public static bool IsValidGuid(this string g){try
{Guid gd = new Guid(g);
return true;}catch (Exception)
{return false;}}}
To use the method, we just make the call directy on the string we have ..example:
And that all there is to it. Now you have an extention method ready to go.string s = "some text or guid string!"Console.writeline(s.IsValidGuid());
No comments:
Post a Comment