This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What does the tilde before a function name mean in C#?
What is the tilde (~) in the enum definition?
I know "~" is for Finalzier methods but now I saw some C# code like this:
if (~IsFieldDeleted(oRptField.GetLayoutField()) != 0)
{
oCollection.Add(oRptField, oRptField.ObjectKeyString);
// some more stuff
}
notice that "~" in the first line?
and then if I go to implementation of IsFieldDeleted it is a method that returns an int.
private int IsFieldDeleted(LayoutLib.LayoutField oLayoutField)
{
Collection oColl = GetFieldIdsForField(oLayoutField);
return (oColl.Count == 0) ? 1 : 0;
}
The ~ operator performs a bitwise complement.
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/bitwise-complement-operator
IsFieldDeleted() returns an int, which is a type to which that operator can be applied (int, uint, long, ulong). The bitwise complement is taken and then compared to zero.
I don't see how the if(...) can ever be true, since IsFieldDeleted() only returns 0 or 1 and ~0 and ~1 are both not zero.
Related
This question already has answers here:
C# String Operator Overloading
(7 answers)
Closed 1 year ago.
Personally, I find the string.CompareTo() annoying to use and read.
I would love to read and write like: if (string1 > string2)
Currently, I can do this, using ExtensionMethods:
string a = "ABC";
string b = "BBC";
if ( a.IsGreaterThan(b) )
{
Console.WriteLine("a is greater than b");
}
else if ( a.IsLessThan(b) )
{
Console.WriteLine("a is less than b");
}
public static class StringExtensions
{
public static bool IsGreaterThan(this string i, string value)
{
return (i.CompareTo(value) > 0);
}
public static bool IsLessThan(this string i, string value)
{
return (i.CompareTo(value) < 0);
}
}
This is much better, but I'd still like to define the < and > operators. I found an OLD post explaining why this is NOT possible:
Currently this is not supported because Extension methods are defined in separate static class and static classes cannot have operator overloading definitions.
Has anything changed in C# to now allow defining the < and > operators for string class?
I'd still like to define the < and > operators
But why [.gifv]?
What does it mean to you that a string is "greater than" another? Its length? Or the first Unicode codepoint within it that's higher than the codepoint at the same location in the first string?
What about combined characters? Is ñ (U+00F1) greater or smaller than n and ~ combined (U+006E and U+0303), or perhaps equal? What about i + j and ij? Would the current culture matter?
Be happy that there's such a "clumsy" method to do comparisons, so you can call an overload on it to specify what kind of comparison you actually want. There is no one size fits all in string comparisons, and you definitely shouldn't be able to override it for all strings.
Besides, already compiled code doesn't care about extension methods (or extension operators, if that were a thing). Consider you're running library code in which you can definitely read that there's a if (foo < bar) and in your code it's true, but in the compiled code it's false, because it does a different comparison for the same operator.
So no, C# doesn't allow overriding operators for built-in types.
I was browsing around stack overflow and I encountered this question:
check for duplicate filename when copying files in C#
In this question, this little gem existed:
int i = +1
I have never seen this syntax before. So I opened up the interactive C# window in visual studio:
Microsoft (R) Roslyn C# Compiler version 1.3.4.60902
Loading context from 'CSharpInteractive.rsp'.
Type "#help" for more information.
> int i = +1;
> i
1
> +1 == 1
true
Is this similar to +=? Is this some new syntax? What is this operator? How is it different than a normal variable declaration?
That's the unary plus operator. From the documentation:
The result of a unary + operation on a numeric type is just the value of the operand.
In most sane contexts1 where you're writing code, it'll be optional (+1 is the same as 1 if we're writing literals).
It mostly exists for symmetry with the unary minus operator.
Most of the time, you'll not write code containing it, but if you're generating code it can be handy to be able to apply a unary operator either way2.
It has no relation to +=.
1Insane code could override this operator for custom types and make it more than a no-op. But I'd love to understand a use case where it makes code more understandable, which should be the main aim of most code.
2E.g. imagine you're chaining a set of operations together and for each additional element, you wish to change the sign of the overall result. This lets you just store an operator and apply it blindly when you finally decide to output a result
For for all signed numeric types the positive-sign is optional. So,
+1 == (+1) == 1
+1.0 == (+1.0) == 1.0
+1L == (+1L) == 1L
+1.0m == (+1.0m) == 1.0m
Do not confuse
int i = +1; // Assigns 1
which is the same as
int i = (+1); // Assigns 1
or simply
int i = 1; // Assigns 1
with
int i += 1; // INCREMENT!
which increments i.
In C# terms there is a binary + operator (the addition operator as in int i = 3 + 4;) and a unary + operator (the plus sign as in int i = +1;).
Think of it the way you think of
int i = -1
and it becomes obvious
This question already has answers here:
Operator '==' cannot be applied to operands of type 'string' and 'int' [closed]
(2 answers)
Closed 4 years ago.
if (Properties.Settings.Default.BG == 1)
{
}
I think there is something I need to write before (1) but I don't know
To represent a string, you need to surround the value with double quotes. Currently you're comparing it to an integer. Instead, do something like this:
if (Properties.Settings.Default.BG == "1")
{
}
Another way to do this would be to call the built-in ToString() method that belongs to all objects:
if (Properties.Settings.Default.BG == 1.ToString())
{
}
This question already has answers here:
How can I convert String to Int?
(31 answers)
Closed 7 years ago.
I need to display a certain number on a label when the user input is between >=0 and <= 1. User input is a string and I need the numbers between to be a decimal or double. Obviously I can't compare the two like I did because the operators can't compare a string and decimal, double, or int.
private void voltageTextBox_TextChanged(object sender, EventArgs e)
{
var RTPower = powerTextBox.Text;
powerTextBox.CharacterCasing = CharacterCasing.Upper;
if (RTPower >= 0 && <= 1)
{
displayLabel4.Text = "1";
}
}
Error: Operator '>=' cannot be applied to operands of type 'string' and 'int'
Error: Operator '<=' cannot be applied to operands of type 'string' and 'int'
How can I make that if statement work? Do I have to keep it as a string, display it in label, convert the label to an integer then re-display it? I know I can make that work, but that is far too complicated. I just need an easier way to do this.
int RTPower = Int32.Parse(powerTextBox.Text);
or for decimal values
decimal RTPower = Decimal.Parse(powerTextBox.Text);
You need to convert the value from a string to an int.
Also, I assume you are new to c# - my advice would be to avoid using var and explicitly declare your variables. It will make things clear and easier for you to learn and understand.
You can convert to int like
int x = Int32.Parse(RTPower);
then you can compare x.
If, however, you know that the user input will be between [0, 9] then you could use
if(RTPower >= "0" && <= "1")
because strings are compared lexicographically so "1" is under "9" but "10" is under "2".
The first way is much better though, because it works for all numerical user inputs
This question already has answers here:
Is it possible to create a new operator in c#?
(7 answers)
Closed 8 years ago.
Is it possible to create a custom operator like '!?' (negation of '??') instead of writing long expression:
int? value = 1;
var newValue = value != null ? 5 : (int?)null;
I want to have:
var newValue = value !? 5;
Thanks!
No.
You cannot create your own operators in C#. You can only override (some of the) ones that already exist in the language.
You cannot define new operator in C# - the only way is to redefine existing ones, but you can not redefine ?: and ?? operators.