Things you probably didn’t know about C# – Part 2

Date: 4 Mar 2010 Comments: 0

After a post about Things you probably didn’t know about C#, here’s a new post from the series:

1. If you want to exit you app without calling any finalizers or finally blocks use:

Environment.FailFast();

2. Instead of

sting filePath = directoryPath + “\” + filename;

you can use:

sting filePath = System.IO.Path.Combine(directoryPath, filename);

3. Forget about “rn” for new line, you can use Environment.NewLine instead. It’s better because it’s system independent

4. You can create a typed reference from a variable:

int i = 100;

TypedReference typeRef = __makeref(i);

5. From the example above, you can extract the original type:

Type type = __reftype(typeRef);

6. You can extract the value from TypedReference:

int j = __refvalue(typeRef, int);

7. You can use the ‘@’ character in the beginning of variable names. It’s suitable to be used for vars:

var @obj = new object();

8. If you want the garbage collector to collect some object while it can be accessed by the application, you can use the System.WeakReference class, you can read more about it HERE.

P.S. I’ve test all the examples above under .NET Frawork 3.5.

Leave a Reply