Your Ad Here

Things you probably didn’t know about C#

Date: 25 Feb 2010 Comments: 2 so far
Warning: file_get_contents() [function.file-get-contents]: URL file-access is disabled in the server configuration in D:\Hosting\3681091\html\blog\wp-content\plugins\digg-digg\dd.class.php on line 759

Warning: file_get_contents(http://feeds.delicious.com/v2/json/urlinfo/data?url=http%3A%2F%2Fwww.devtheweb.net%2Fblog%2F2010%2F02%2F25%2Fthings-you-probably-didnt-know-about-csharp%2F) [function.file-get-contents]: failed to open stream: no suitable wrapper could be found in D:\Hosting\3681091\html\blog\wp-content\plugins\digg-digg\dd.class.php on line 759

I had some free time today, so I decided to learn something new in C#, here’s list of most interesting things that I learned:

1. Instead of

using (Font f1 = new Font(“Arial”, 10.0f))
{
using (Font f2 = new Font(“Calibri”, 10.0f))
{
//t use f1 and f2 here
}

}

it can be written:

using (Font f1 = new Font(“Arial”, 10.0f), Font f2 = new Font(“Calibri”, 10.0f))
{
//t use f1 and f2 here
}

2. Instead of

bool NameExists(string name) {

return name == “dev” || name == “the” || name == “web” ||

name == “.net”;

}

it can be written:

bool NameExists(string name){

return new[] { “dev”, “the”, “web”, “.net” }.Contains(name);

}

3. You can compare strings ignoring cases in this way str1.ToUpper() == str2.ToUpper(), but it makes additional string allocation, better way is to use OrdinalIgnoreCase: str1.Equals(str2, StringComparison.OrdinalIgnoreCase)

4. The ?? operator can be chained in a bunch of null comparisons:

string result = var1 ?? var2 ?? var3 ?? String.Empty;

5. In the following statement: SomeClass obj = (SomeClass) obj;  If obj is not SomeClass, a class cast exception will be thrown, if you want to return null if obj is not SomeClass, you can use the as operator: SomeClass obj = obj as SameClass;

6. I don’t thinks it’s recommended, but you can use Unicode characters in names, ex.

public string ТихомирIvanov()

{

return “My name is Tihomir Ivanov”;

}

But more interesting is that you can also use Unicode escapes, ex.

public string T\u0418хомирIvanov ()

{

return “My name is Tihomir Ivanov”;

}

7. In C# you can create JavaScript-like anonymous inline functions, ex.

var var1 = new Func<String>(() =>

{

return “DevTheWeb.NET”;

})();

8. Let’s we have the following  generic method declaration:

public void Func<T>(T value);

We can call it in this way: Func<int>(1);

But we can also in this way:

Func(5);
Note: The second way of calling a generic method won’t work if we’re calling the method through reflection.

That’s all. I hope you’ve found something useful in the examples above. Next Thursday, I’ll add new post about interesting things in C#.

  1. 2 Comments to “Things you probably didn’t know about C#”

    1. zproxy says:

      Did not know about #6

    2. Amry says:

      Nice list. I think it’s better to state that you are referring to C# 3.0.

      Additional note for #5: if you simply want to check if obj is SomeClass, then you can use if (obj is SomeClass), or if (!(obj is SomeClass)) for false check.

      #6: Also did not know about this.

    Leave a Reply


    Spam protection by WP Captcha-Free