C# Function Library - Possible?
I am new to C#, using VS Net 2003. As I make applications, I would like to add a library of oft used user defined functions; IsStringEmptyorNull(), IsStringNumeric(), etc.
It seems that C# promotes class libraries rather than function libraries. I can put these functions into classes, but it seems clumsy; having to instantiate a class then call a method.
Is there a way to make a function library so that I can just make an inline call like
if (IsStringEmptyorNull(strMyString))
{
.. some code here
}
Thanks
Mike Thomas

C# Function Library - Possible?
kanlinkan
You can do this using static methods so that you don't have to instantiate a class to use the method. For example I would create a class like following:
public class Library
{
public static bool IsStringEmptyOrNull(string s)
{
if (s == null || s.Trim() == string.Empty)
return true;
else
return false;
}
}
And it's usage would look something like:
if (Library.IsStringEmptyOrNull("Test"))
{
//...
}
BjarneSCN
you don't have to instantiate the class if you declare the methods as static:
class MyLib
{
public static bool IsStringEmptyOrNull(String str)
{
// some code
}
}
To call the method you'd do:
MyLib.IsStringEmptyOrNull(myString);
Jetjez
That's true, but I'll have many more UDF's before this is all over.
Mike Thomas
Laurel Hale
First I'd like to point out that all of these functions are available in .Net 2.0 (you can download and install Visual C# 2005 express for free).
If you want to create your own function library. I'd suggest making a class that has all static methods.
Public Class Utils
{
}
Then you can access those methods without creating the Utils class like so
if(Utils.IsStringEmptyorNull(strMyString))
{
//do something
}
Search the MSDN index for static to get more info.