What does = operator mean in a property in C#?
This is an expression-bodied property, a new syntax for computed properties introduced in C# 6, which lets you create computed properties in the same way as you would create a lambda expression. This syntax is equivalent to
public bool property {
get {
return method();
}
}
Similar syntax works for methods, too:
public int TwoTimes(int number) => 2 * number;
What does the = operator mean in a property?
What you're looking at is an expression-bodied member not a lambda expression.
When the compiler encounters an expression-bodied property member, it essentially converts it to a getter like this:
public int MaxHealth
{
get
{
return Memory[Address].IsValid ? Memory[Address].Read<int>(Offs.Life.MaxHp) : 0;
}
}
(You can verify this for yourself by pumping the code into a tool called TryRoslyn.)
Expression-bodied members - like most C# 6 features - are just syntactic sugar. This means that they don’t provide functionality that couldn't otherwise be achieved through existing features. Instead, these new features allow a more expressive and succinct syntax to be used
As you can see, expression-bodied members have a handful of shortcuts that make property members more compact:
- There is no need to use a
return
statement because the compiler can infer that you want to return the result of the expression - There is no need to create a statement block because the body is only one expression
- There is no need to use the
get
keyword because it is implied by the use of the expression-bodied member syntax.
I have made the final point bold because it is relevant to your actual question, which I will answer now.
The difference between...
// expression-bodied member property
public int MaxHealth => x ? y:z;
And...
// field with field initializer
public int MaxHealth = x ? y:z;
Is the same as the difference between...
public int MaxHealth
{
get
{
return x ? y:z;
}
}
And...
public int MaxHealth = x ? y:z;
Which - if you understand properties - should be obvious.
Just to be clear, though: the first listing is a property with a getter under the hood that will be called each time you access it. The second listing is is a field with a field initializer, whose expression is only evaluated once, when the type is instantiated.
This difference in syntax is actually quite subtle and can lead to a "gotcha" which is described by Bill Wagner in a post entitled "A C# 6 gotcha: Initialization vs. Expression Bodied Members".
While expression-bodied members are lambda expression-like, they are not lambda expressions. The fundamental difference is that a lambda expression results in either a delegate instance or an expression tree. Expression-bodied members are just a directive to the compiler to generate a property behind the scenes. The similarity (more or less) starts and end with the arrow (=>
).
I'll also add that expression-bodied members are not limited to property members. They work on all these members:
- Properties
- Indexers
- Methods
- Operators
Added in C# 7.0
- Constructors
- Finalizers
However, they do not work on these members:
- Nested Types
- Events
- Fields
What does = operator pointing from field or a method mean? C#
These are Expression Body statements, introduced with C# 6. The point is using lambda-like syntax to single-line simple properties and methods. The above statements expand thusly;
public int Calculate(int x)
{
return DoSomething(x);
}
public void DoSoething()
{
SomeOtherMethod();
}
Notably, properties also accept expression bodies in order to create simple get-only properties:
public int Random => 5;
Is equivalent to
public int Random
{
get
{
return 5;
}
}
What does = do in .Net C# when declaring a property?
The first uses the new-to-C#-6 expression-bodied member syntax. It's equivalent to:
public object MyObject
{
get { return new object(); }
}
The second is also new to C# 6 - an automatically implemented read-only property. It's equivalent to:
private readonly object _myObject; // Except using an unspeakable name
public object MyObject
{
get { return _myObject; }
}
You can only assign to MyObject
from within a constructor in the declaring class, which actually just assigns to the field instead.
(Both of these "equivalencies" are using old-school property declarations, where you always have get
, set
or both as blocks containing code.)
What does | operator mean in expression-bodied property?
It`s a bitwise or. If we look at the values in your code you could have:
- 0 =>
00
- 1 =>
01
- 2 =>
10
So, if booTrue
is true and fooTrue
is true, it will be 1 | 2
. As it's a bitwise or, it will be 01 | 10
=> 11
in decimal => 3 .
What does ?. mean in angular 5?
?
Means safe navigation operator
From Docs
The Angular safe navigation operator (?.) is a fluent and convenient
way to guard against null and undefined values in property paths. Here
it is, protecting against a view render failure if the currentHero is
null.
This specifically means that if the value you are binding to the view is null then it should return null else the actual value, so that you dont get any issues while rendering the template.
In the above sample code you provided ,
product?.id.name
it checks whether produce object exists and then it will check if there is an id.
since you dont have ?
after id. it will throw an error "cannot read property of 'name' undefined".
What does question mark and dot operator ?. mean in C# 6.0?
It's the null conditional operator. It basically means:
"Evaluate the first operand; if that's null, stop, with a result of null. Otherwise, evaluate the second operand (as a member access of the first operand)."
In your example, the point is that if a
is null
, then a?.PropertyOfA
will evaluate to null
rather than throwing an exception - it will then compare that null
reference with foo
(using string's ==
overload), find they're not equal and execution will go into the body of the if
statement.
In other words, it's like this:
string bar = (a == null ? null : a.PropertyOfA);
if (bar != foo)
{
...
}
... except that a
is only evaluated once.
Note that this can change the type of the expression, too. For example, consider FileInfo.Length
. That's a property of type long
, but if you use it with the null conditional operator, you end up with an expression of type long?
:
FileInfo fi = ...; // fi could be null
long? length = fi?.Length; // If fi is null, length will be null
Related Topics
How to Create a Dropdownlist from an Enum in ASP.NET MVC
How to Remove All Event Handlers from an Event
What's the Difference Between Invoke() and Begininvoke()
How to Decode HTML Characters in C#
Remove Duplicates from a List≪T≫ in C#
How to Ignore a Property in Class If Null, Using Json.Net
Easiest Way to Read from and Write to Files
C# Difference Between == and Equals()
When Should I Use a Struct Rather Than a Class in C#
Process.Start: How to Get the Output
Transitioning from Windows Forms to Wpf
Processstartinfo Hanging on "Waitforexit" - Why