0

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;

}
Community
  • 1
  • 1

1 Answers1

2

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.

Lauren Rutledge
  • 1,195
  • 5
  • 18
  • 27
Eric J.
  • 147,927
  • 63
  • 340
  • 553
  • `IsFieldDeleted` returns either `0` or `1`. Their complement is never zero. Perhaps this is a bug in the original code? – John Dvorak Dec 12 '12 at 20:52
  • @JanDvorak: Funny I was updating my post as you typed the comment. Agree 100%. – Eric J. Dec 12 '12 at 20:52
  • @EricJ. : Thanks for inmfo but when I put a break point inside the commands of that if(...) block it hits the break point. –  Dec 12 '12 at 20:55
  • @user1899082: Try assigning the return value of `IsFieldDeleted()` to an int variable and share what that value is. – Eric J. Dec 12 '12 at 21:43
  • @EricJ. IsFieldDeleted was zero so its negate was negative one so it went inside the if block . –  Dec 12 '12 at 21:52