Using static method from other file without class name [duplicate]
I tried to use something like using Printer; but it doesn't work.
I just want use P(); instead of Printer.P();
File Printer.cs
using System;
namespace animals
{
public class Printer
{
public static void P()
{
P("\a");
}
public static void P(object val, bool doEnterAfterLine = true, bool printInOneLine = false)
{
P(val.ToString());
if (doEnterAfterLine)
Console.WriteLine(); //enter
}
static void P(bool printSeparator = false)
{
if (printSeparator == false)
return;
P("---------------------------------------------------------", true);
}
static void P(string value = "none", bool modifyColor = false, bool ding = false, bool printInOneLine = false)
{
var oldColor = Console.ForegroundColor;
if (modifyColor)
Console.ForegroundColor = ConsoleColor.Magenta;
if (printInOneLine)
Console.Write(value + " ");
else
Console.WriteLine(value);
Console.ForegroundColor = oldColor; //recover color
if (ding)
{
Console.Write("\a");
}
}
}
}
File Program.cs
using System;
// this resolve problem: using static animals.Printer;
namespace animals
{
class Program
{
static void Main(string[] args)
{
Printer.P(); // it works
P(); // info: does not exist in current context
}
}
}
Solution 1:
Use using static <namespace>.<class>;
to access the members of <class>
without qualifying its name.
using static animals.Printer;