简体   繁体   English

如何在没有任何参数的情况下创建和调用方法?

[英]How can i make and call a method without any paramaters?

I want to simply call my method like this: collect.clear;我想简单地像这样调用我的方法:collect.clear; instead of, collect.clear();而不是 collect.clear();

in other words, I want to make this method换句话说,我想做这个方法

    class collect
    {
       public List<string> list = new List<string>();
       public void Clear()
       {
           list.clear();
       }
    }

to be called like so被这样称呼

    static void Main(string[] args)
    {
        collect.clear;
    }

is this possible or not at all.这可能还是根本不可能。

I want to simply call my method like this: collect.clear;我想简单地像这样调用我的方法:collect.clear; instead of, collect.clear();而不是 collect.clear();

Well, frankly: you don't get to decide what the language syntax is, and in C#, the syntax for invoking a method is: collect.clear();好吧,坦率地说:您无法决定语言语法是什么,而在 C# 中,调用方法的语法是: collect.clear(); . .

Basically, you can't do what you want.基本上,你不能做你想做的。 You could make it a property, but then you'd need to discard the result (so it can choose between get and set ), ie with a property get called clear , _ = collect.clear;可以将其设为属性,但随后您需要丢弃结果(因此它可以在getset之间进行选择),即具有名为clear属性 get , _ = collect.clear; - frankly I think that's a step back from the () . - 坦率地说,我认为这是从()退后一步 It is also a terrible idea from the basis of unexpected side-effects;从意想不到的副作用的基础上,这也是一个可怕的想法; most UI elements (including the debugger) and libraries (serializers, etc) think that they can freely evaluate property gets, so it would be very unexpected it reviewing a property had the side effect of clearing the data!大多数 UI 元素(包括调试器)和库(序列化器等)都认为它们可以自由地评估属性获取,因此查看属性具有清除数据的副作用是非常出乎意料的! Basically, don't do that.基本上,不要这样做。

So;所以; embrace the () .拥抱() They express the intent here, for your benefit, the benefit of people reviewing/maintaining it, and for the benefit of the compiler.他们在这里表达了意图,为了您的利益,为了审查/维护它的人的利益,以及为了编译器的利益。

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

相关问题 如何使用参数在 C# 中调用 Oracle 连接方法? - How can i call a Oracle connection method in C# with paramaters? 如何在没有任何身份验证的情况下调用 WCF 服务? - How can I call the WCF service without any authentication? 如何制作在加载任何场景之前运行的方法? - How can I make a method that runs before any scene is loaded? c#调用参数为字符串变量的方法 - c# call a method with paramaters as a string variable 我可以使用任何类型的内联构造来使外部方法调用等待吗? - is there any type of inline construct I can use to make an external method call awaitable? 如何从类中调用方法,但如何从任何接口实现此方法? - How can i call method from class but this method implemented from any interface? 如何在没有停止的情况下在另一个异步方法中创建调用异步方法 - How can I create call an async method inside another async method without it stalling 如何制作一个可以执行传递给它的任何其他方法的方法 - How to make a method that can execute any other method passed to it 如何在对异步方法的调用上追加对扩展方法的调用 - How can I append a call to an extension method on a call to an async method 如何在不通知父类该泛型类型的情况下在注入的类上调用泛型方法? - How can I call a generic method on an injected class without informing the parent class of the generic type?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM