简体   繁体   English

如何调用类而不是类内的方法?

[英]How to call a class rather than a method inside a class?

so I want to make it easy to call Console.WriteLine() (from the namespace system) so I wrote:所以我想让调用Console.WriteLine() (来自命名空间系统)变得容易,所以我写道:

using System;

namespace main
{
    namespace easier
    {
        public class print
        {
            public print(string input)
            {
                Console.WriteLine(input);
            }
        }
    }
}

But when I try to call print("Hello World); it says CS1955: Non-invocable member 'print' cannot be used like a method .但是当我尝试调用print("Hello World);它说CS1955: Non-invocable member 'print' cannot be used like a method

Is there a way so I don't have to do class.print("Hello World");有没有办法让我不必做class.print("Hello World"); just print("Hello World");只需print("Hello World"); ? ?

Thanks in advance!提前致谢!

You can use using static您可以使用using static

using System;
using static Easier;

class Program
{
    static void Main(string[] args)
    {
        Print("message");
    }
}

public static class Easier
{
    public static void Print(string value)
    {
        Console.WriteLine(value);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM