What does object?.Property mean in c# [duplicate] - c#

This question already has answers here:
What does question mark and dot operator ?. mean in C# 6.0?
(3 answers)
Closed 6 years ago.
I recently came across this while going through someone else's code
var name = Product.Buyer?.FirstName + " " + Product.Buyer?.LastName;
What does this(?.)mean in c#

The operator ?. is called Null-conditional Operators, which is introduced in C# 6.0.
Used to test for null before performing member access (?.) or index
(?[) operation. These operators help you write less code to handle
null checks, especially for descending into data structures.
see the documentation and an example here

Related

Understanding the null conditional operator [duplicate]

This question already has answers here:
What is the purpose of a question mark after a value type (for example: int? myVariable)?
(9 answers)
What does the ? operator mean in C# after a type declaration?
(7 answers)
Closed 3 years ago.
I am reading this article and it has this example
int? length = people?.Length; // null if people is null
My question is if the first question mark after the int a typo ? If not what is the purpose of introducing a ? after an int. I understand why its introduced after people. The reason for that is if people is null then it will return a null. Can someone explain the purpose of the ?

What does the "?." operator do in c#? [duplicate]

This question already has answers here:
What does question mark and dot operator ?. mean in C# 6.0?
(3 answers)
Closed 5 years ago.
I've encountered this operator in C# when dealing with custom events: MyEvent?.Invoke(this, new EventArgs());. What is the purpose of the ?. portion of this statement?
It's making sure it's not null. They have these in swift and their called optionals. If the variable is null then it returns null

How does the tilde operator work in c# [duplicate]

This question already has answers here:
What does the tilde mean in an expression? [duplicate]
(6 answers)
Closed 7 years ago.
intStyle = intStyle & ~(WS_MINIMIZE);
It is the first time I see this , I am trying to learn how to hook low lvl APIs to C# and make some calls , and I do not understand what this line means. Thank you guys!
It's an operation on a flag. You need to understand bitoperations (AND, OR, NOT, XOR..) for that. This line deletes the flag WS_MINIMIZE from the intStyle flagmask. More reading: Using Bitwise operators on flags , http://www.codeproject.com/Articles/13740/The-Beginner-s-Guide-to-Using-Enum-Flags.
See this for the & operator.
And this for the ~ operator
They are bitwise operators. The first one is a bitwise AND. The second one performs a bitwise complement operation.
This is a bitwise operation.
See for example http://www.codeproject.com/Articles/544990/Understand-how-bitwise-operators-work-Csharp-and-V

What is the VB.NET equivalent of the C# ? operator? [duplicate]

This question already has answers here:
Is there a conditional ternary operator in VB.NET?
(5 answers)
Closed 9 years ago.
What is the VB.NET equivalent of the C# ? operator?
For example, how would the following code be written in VB.NET?
hp.pt = iniFile.GetValue("System", "PT").ToUpper().Equals("H") ? PT.PA : PT.SP
Historically, IIf was commonly used for that - but that does not use short-circuiting so is not quite the same. However, there is now a 3-part If:
hp.pt = If(iniFile.GetValue("System", "PT").ToUpper().Equals("H"), PT.PA, PT.SP)
that does use short-circuiting, and thus is identical to the conditional operator in C#.
You can use the If operator
hp.pt = If(iniFile.GetValue("System", "PT").ToUpper().Equals("H"), PT.PA, PT.SP)
Try using the If function like so:
x = If(condition, trueValue, falseValue)
This question is a duplicate of a question that has already been asked and answered:
Is there a conditional ternary operator in VB.NET?
here:
Dim foo as String = If(bar = buz, cat, dog)

What does the ? mean after a type? [duplicate]

This question already has answers here:
Closed 13 years ago.
Possible Duplicate:
a curious c# syntax
So I've seen some code around and a few of them use a ? after the type, like this:
private Point? loc = null;
So I'm wondering if Point? is different than Point (can't put a question mark at the end of my sentence or I'll confuse you guys ... :] ). The language I'm using is C# by the way.
T? is a shorthand (in C#) for Nullable<T> - so Point? is another way of writing Nullable<Point> or example.
See sections 1.3 and 4.1 of the C# 3 language spec - and various other places, to be honest - for more details. See the docs for System.Nullable<T> for more information from the framework side of things. Or read chapter 4 of C# in Depth :) (Unfortunately it's not one of the free chapters.)
(This question is bound to be a duplicate, but I don't have the energy to find it right now.)
Point? is the same as Nullable<Point>. It allows you to assign null to value types, such as structs.
It means the type can accept its' value and null.

Categories

Resources