How can I import a static method from another c# source file and use it without "dots"?
like :
foo.cs
namespace foo
{
public static class bar
{
public static void foobar()
{
}
}
}
Program.cs
using foo.bar.foobar; <= can't!
namespace Program
{
class Program
{
static void Main(string[] args)
{
foobar();
}
}
}
I can't just foobar();
, but if I write using foo;
on the top and call foobar()
as foo.bar.foobar()
it works, despite being much verbose. Any workarounds to this?
With C# 6.0 you can.
C# 6.0 allows static import (See using Static Member)
You will be able to do:
using static foo.bar;
and then in your Main
method you can do:
static void Main(string[] args)
{
foobar();
}
You can do the same with System.Console
like:
using System;
using static System.Console;
namespace SomeTestApp
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Test Message");
WriteLine("Test Message with Class name");
}
}
}
EDIT: Since the release of Visual Studio 2015 CTP, in January 2015, static import feature requires explicit mention of static
keyword like:
using static System.Console;