Use static function from a class without naming the class

前端 未结 4 1744
轻奢々
轻奢々 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条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-20 17:24

    In C#? Not possible. Because it's a full OOP programming language and it was designed to work with objects you can't use functions outside the scope of an object. When calling static methods you have to specify the class where that static method lives...

    Class.StaticMethod();
    

    you can only use the short-hand notation if this method is call from within the same class...

    StaticMethod();
    

    But remember that you will not get access to instance members because static methods donot belong to instance of an object

    Update based on comment

    Looks like it will be possible to call static members without having to specify the class that declares it in C# 6, and you will be able to reference classes directly in using statements...in similar fashion to Java...more info here

提交回复
热议问题