Use static function from a class without naming the class

前端 未结 4 1702
轻奢々
轻奢々 2021-01-20 16:54

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 hopin

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-20 17:01

    If you're looking to define a globally-scoped procedure then the short answer is no, you can't do this in c#. No global functions, procedures or objects.

    In C# everything apart from namespaces and types (class, struct, enum, interface) must be defined inside a type. Static members (fields, properties and methods) can be used without an instance of the class, but only by referencing the type that owns them. Non-static members need an instance of the owning class.

    This is fundamental to the syntax of the language. C# is neither C nor C++, where you can define global objects, functions and procedures.

提交回复
热议问题