The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 4,262 other subscribers

Archive for May, 2010

#fail: Facebook has changed their mail domain to facebookmail.com

Posted by jpluimers on 2010/05/28

Recently, facebook started to complain that my email is not valid any more:

Please update your email address
Our systems have detected that valid.email@example.com is no longer a valid email. Facebook requires all users to maintain an active contact email. Please enter and confirm a new contact email below:
New Email: XXXXXXXX
If you believe you have received this message in error, please reconfirm your current email.

Note I changed my facebook email address in the quote above and log below, but the unchanged one is valid, and accepts mail. Read the rest of this entry »

Posted in Opinions, Power User | 11 Comments »

.NET/C#: comparison is a 101, or isn’t it?

Posted by jpluimers on 2010/05/26

In C# (well, actually: .NET), there are a couple of ways to compare things:

  • the built-in equality operator (==) which translates to the IL instruction CEQ
  • an overloaded equality operator (operator ==) that a class or struct can introduce
  • the virtual Equals method that is introduced in System.Object and which you need to call explicitly

Sometimes, this leads to confusion, for instance, given

object one = 0;
object two = 0;
bool same = one == two;

what would you expect the value of same to be?

Actualy, the value is false.

Why?

The reason is that the operator == does not call the System.Object.Equals method.
So the System.Int32.Equals method is never called.

What happens in stead, is that the CEQ instruction is called.
Since both the integer zero values have been boxed into two objects (referred to by one and two), the CEQ will perform the comparison on the object references.
Even though both zero values are the same, both objects have a reference to a different spot in memory (the contents of both memory spots then are the same zero value).
Since one and two refer two different memory locations, the value of same becomes false.

Similar things hold for most types, unless they overload the operator ==.

System.String is a special type.
Even though it not a value type (it is derived from System.Object, not SystemValueType), it behaves like a value type.

One of the things that string does is overload the operator == (and overload the operator !=):

public static bool operator ==(string a, string b)
{
    return Equals(a, b);
}

So even though one and two are potentially* referring to different locations, their content is compared, and same becomes true.

string one = "Foo";
string two = "Foo";
bool same = one == two;

*Now lets see these memory locations:

object one = "Foo";
object two = "Foo";
bool same = one == two;

Surprise! Here too, same becomes true.

What happened here is that when the compiler compiles two identical strings into an assembly, they are interned and end up at the same memory location.

Conclusion:

Comparison in .NET is not always 101.
It depends on the types involved, value types versus reference types, boxing, interning and the overloaded operator ==.

–jeroen

via The Lounge – CodeProject.

PS:
Another special thing about strings is that they are immutable:

A String object is called immutable (read-only) because its value cannot be modified once it has been created. Methods that appear to modify a String object actually return a new String object that contains the modification. If it is necessary to modify the actual contents of a string-like object, use the System.Text.StringBuilder class.

via String Class (System).

PS2:

Some other interesting links on comparison, interning, etc:

Posted in .NET, C#, Development, Software Development | Leave a Comment »

Happy Towel Day

Posted by jpluimers on 2010/05/25

Don’t forget to bring your Towel today

–jeroen

Posted in .NET, About, Delphi, Development, Opinions, Personal, Software Development | 1 Comment »

“Yoda Conditions” (from: stackoverflow – New programming jargon you coined?)

Posted by jpluimers on 2010/05/25

Having done quite a bit of C and C++ work in the past, I often still use “Yoda Conditions”, especially in environments where you have both = and == as an operator.
So, in a boolean expression, I often put the constant to test in front of the test.

I recently learned at stackoverflow that quite a few people call these “Yoda Conditions”:

“Yoda Conditions”— the act of using if(constant == variable) instead of if(variable == constant), like if(4 == foo). Because it’s like saying “if blue is the sky” or “if tall is the man”.

Thanks to dreamlax for helping me find that.

This is a problem in languages that have both the = and == operators, and the result of an assignment itself is also a value (i.e. allowing a = b = true). Read the rest of this entry »

Posted in .NET, C#, Delphi, Development, Java, Ruby, Software Development, Web Development | 22 Comments »

Visual Studio 2010: macro to change Target Framework Version for solution (by Scott Dorman)

Posted by jpluimers on 2010/05/21

Scott Dorman udpated his macro to change the target framework version for all projects in a solution to Visual Studio 2010 and published the new macro on CodeProject.

His new macro now supports these target frameworks:

Notes:

  • The links are to the download pages of the frameworks; look for “Standalone version” or “Full installer” for non-bootstrap download.
    (version 1.1 can be downloaded here, but is not supported in VS2010)
  • The “Client Profile” versions are stripped down versions of their “Full” counterpart.

–jeroen

Posted in .NET, C#, C# 2.0, C# 3.0, C# 4.0, Delphi, Development, Prism, Software Development, Visual Studio and tools | 2 Comments »