I saw a source code of C# that it was using this syntax for get accessor in a class
public int _f;
public int f
{
get => _f;
}
instead of
public int _f;
public int f
{
get {return _f;}
}
and i was wondering do c# have => operator for simplifying
{return x;}
to
=> x;
if yes what is pre requisties of that? which c# version and which namespaces should be used?
thanks
Now you can do it just if the property is read only, avoiding brackets:
public int MyProperty => 6;
And this has been introduced by the version 6 of the language, so up to C# 5 this won't work either.
For setters as well it will be one of the new features of C# 7 .
It's a new feature of C# 6.0. You can simply write:
public int f => _f;
instead of
public int f
{
get { return _f; }
}
Remember that this is possible only if the property is readonly (it has only get accessor). Of course instead of _f
you can write an expression returning proper type.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.