What does the "?." operator do 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 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

Related

C# short if statement to execute a function if true [duplicate]

This question already has answers here:
Shortest way to check for null and assign another value if not
(12 answers)
Closed 2 years ago.
Is it possible to execute a function within a short statement if the statement is true?
Something like that:
myObject.subObject != null ?? Db.LoadReferences(myObject.subObject); // ORMLite function
yes:
if(myObject.subObject != null) Db.LoadReferences(myObject.subObject);
At just 1 single character more than your original.

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 object?.Property mean in c# [duplicate]

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

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 we call :: in C#? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Scope-resolution operator :: versus member-access operator . in C#
What we call :: in following example?
MyNamespace.Properties.Resources.myImage;
global::MyNamespace.Properties.Resources.myImage;
We call it the namespace alias qualifier.

Categories

Resources