tags:

views:

70

answers:

3

I have a StringUtilities.cs file in the CommonFunctions Project that holds a UppercaseFirst function that Uppercases the first word in a string. Currently in the .aspx.cs in a separate Project (in the same Solution) that is utilizing this function is called using MyProject.CommonFunctions.StringUtilities.UppercaseFirst("hello world");

Is it possible to shorten it to just UppercaseFirst("hello world"); ? Readability will be so much better.

StringUtilities.cs in the CommonFunctions Project:

namespace MyProject.CommonFunctions
{
    public class StringUtilities
    {
        public static string UppercaseFirst(string s)
        {//blah code}
    }
}

Default.aspx.cs

using MyProject.CommonFunctions;
...
protected void Page_Load(object sender, EventArgs e)
{
     MyProject.CommonFunctions.StringUtilities.UppercaseFirst("hello world");
}
+1  A: 

You can't cut it all the way down to just the method name, but given that you've already got the using MyProject.CommonFunction; line in place, you could shorten it to:

StringUtilities.UppercaseFirst("hello world");
Jeromy Irvine
+2  A: 

Not in C#; VB.NET (and C++/CLI) have different lookup rules.

In C#, you can use a using alias as follows:

using StrUtil = MyProject.CommonFunctions.StringUtilities;

which would allow you to then write

protected void Page_Load(object sender, EventArgs e)
{
     StrUtil.UppercaseFirst("hello world");
}

regardless of the enclosing namespace.

Dan
+2  A: 

If you're using c# 3.0 (.NET 3.5) you can use extension methods:

namespace MyProject.CommonFunctions
{
    public static class StringUtilities
    {
        public static string UppercaseFirst(this string s)
        {//blah code}
    }
}

Once you set your usings in appropriate file you can then go:

using MyProject.CommonFunctions;
...
protected void Page_Load(object sender, EventArgs e)
{
     "hello world".UppercaseFirst();
}
Rashack