What does "|=" operator means in C#? [duplicate] - c#

This question already has answers here:
What does |= (single pipe equal) and &=(single ampersand equal) mean
(3 answers)
Closed 7 years ago.
Just walked through referencesource.microsoft.com (line 217)
and discovered "|=" operator. What does it means? I assume it some sort of Boolean operation- but can't get what exactly it means. Does it mean "or equal", short form like "a= a|b" ?

|= is to = what += is to =. Just a shortcut to avoid the writing of a = a | b

its a bitwise inclusive OR and assignment operator.
For details refer
https://msdn.microsoft.com/en-us/library/ms173224.aspx

You are correct. This operator is known as the "OR assignment" operator and a |= b is equivalent to a = a | b. Here is the
documentation.

Related

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 does '<<' do in a C# integer declaration? [duplicate]

This question already has answers here:
C# Left Shift Operator
(4 answers)
Closed 7 years ago.
In C#, what does the << operator do when used to declare a variable?
int layer = 1 << 8;
I've tried looking up the operator, but only confirmed what I already understood. I'm not to sure how it is being applied in a way that is compatible with the integer.
The left-shift operator (<<) shifts its first operand left by the number of bits specified by its second operand. The type of the second operand must be an int or a type that has a predefined implicit numeric conversion to int.

Difference between sbyte and SByte [duplicate]

This question already has answers here:
What is the difference between String and string in C#?
(66 answers)
Closed 7 years ago.
In what way are each intended to be used? Also the differences between them?
For an example:
sbyte myByte = 1;
SByte myByte = 1;
sbyte is an alias of System.SByte. There is no difference between the two. The guidelines state that it is recommended to use the alias sbyte instead of SByte. You can see the MSDN documentation here: https://msdn.microsoft.com/en-us/library/d86he86x.aspx

How are "anded" 'if' conditions evaluated? [duplicate]

This question already has answers here:
Difference between | and || or & and && for comparison [duplicate]
(7 answers)
Closed 8 years ago.
In C#, if we have the following code:
if (condition1 && condition2)
and condition1 turns out to be false, is condition2 still checked or does the execution simply continue after the if statement?
Firstly it evaluates condition1, then if it is true then it will evaluates condition2. It will not evaluate condition2 if condition1 is false. This is called short-circuit evaluation.
No the && operator is short circuiting. The & operator is however not and all of the expressions will be evaluated if you use that.
Yes C# does short circuit evaluation of boolean expressions. Therefore
if ( X && Y() )
1) X will be executed first
2) Y will only be executed if and only if X returns true
This applies to all boolean expressions and not just those in an IF statement...Check this in the C# Specification available online at MSDN. section 14.11.1
you can use also the & and in this case it won't be a short circuite evaluation because
& is the "and" operator used for bit manipulation.
&& is the "and" operator used to evaluate logically expressions.

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)

Categories

Resources