March 1, 2010

.NET 4.0: New API



.NET 4.0 adds several new methods. 
1) Enum.HasFlag  checks  whether a flag field present in the enum instance.


[Flags]
public enum PizzaToppings
{
Onions = 1,
Mushrooms = 2,
Chicken = 4
}
PizzaToppings myToppings = PizzaToppings.Onions |
PizzaToppings.Mushrooms;
 
Assert.IsFalse( myToppings.HasFlag(PizzaToppings.Chicken),
"Chicken is not expected"); 
2) String.IsNullOrWhiteSpace can be useful when whitespace condition needs to be checked along with Empty, and Null conditions.


[Test]
public void TestIsNullOrWhiteSpace()
{
string s = " \t ";
Assert.IsTrue( string.IsNullOrWhiteSpace(s),
"tabbed white spaces");
Assert.IsTrue(string.IsNullOrWhiteSpace(null), "Null");
Assert.IsTrue(string.IsNullOrWhiteSpace(""), "Empty");
Assert.IsTrue(string.IsNullOrEmpty(" \t ".Trim()));
}