[英]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;
您可以将其设为属性,但随后您需要丢弃结果(因此它可以在
get
和set
之间进行选择),即具有名为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.