使用类的静态函数而不命名类 [英] Use static function from a class without naming the class

查看:67
本文介绍了使用类的静态函数而不命名类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从类访问函数而不必每次都命名该类?我知道如何使用使用",因此我不必命名命名空间,但我希望可以使用此静态函数,以便可以像在同一个类中调用函数的方式来调用它们

How can I access functions from a class without having to name that class every time? I know how to use "using" so that I don't have to name the namespace but I was hoping there was a way to do with this static functions so that I can call them the way I would call a function in the same class.

推荐答案

使用静态yournamespace.yourclassname;

using static yournamespace.yourclassname;

然后调用不带类名的静态类方法;

then call the static class method without class name;

示例:

Class1.cs

Class1.cs

namespace WindowsFormsApplication1
{
    class Utils
    {
        public static void Hello()
        {
            System.Diagnostics.Debug.WriteLine("Hello world!");
        }
    }
}

Form1.cs

using System.Windows.Forms;
using static WindowsFormsApplication1.Utils;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
            Hello();    // <====== LOOK HERE
        }
    }
}

这篇关于使用类的静态函数而不命名类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆