Getting the thread ID from a thread

GetThreadId returns the ID of a given native thread. There are ways to make it work with managed threads, I’m sure, all you need to find is the thread handle and pass it to that function. GetCurrentThreadId returns the ID of the current thread. GetCurrentThreadId has been deprecated as of .NET 2.0: the recommended way … Read more

When should I use GC.SuppressFinalize()?

SuppressFinalize should only be called by a class that has a finalizer. It’s informing the Garbage Collector (GC) that this object was cleaned up fully. The recommended IDisposable pattern when you have a finalizer is: public class MyClass : IDisposable { private bool disposed = false; protected virtual void Dispose(bool disposing) { if (!disposed) { … Read more

Ignoring a class property in Entity Framework 4.1 Code First

You can use the NotMapped attribute data annotation to instruct Code-First to exclude a particular property public class Customer { public int CustomerID { set; get; } public string FirstName { set; get; } public string LastName{ set; get; } [NotMapped] public int Age { set; get; } } [NotMapped] attribute is included in the … Read more

Nested using statements in C#

The preferred way to do this is to only put an opening brace { after the last using statement, like this: using (StreamReader outFile = new StreamReader(outputFile.OpenRead())) using (StreamReader expFile = new StreamReader(expectedFile.OpenRead())) { ///… }

How to rethrow InnerException without losing stack trace in C#?

In .NET 4.5 there is now the ExceptionDispatchInfo class. This lets you capture an exception and re-throw it without changing the stack-trace: using ExceptionDispatchInfo = System.Runtime.ExceptionServices.ExceptionDispatchInfo; try { task.Wait(); } catch(AggregateException ex) { ExceptionDispatchInfo.Capture(ex.InnerException).Throw(); } This works on any exception, not just AggregateException. It was introduced due to the await C# language feature, which unwraps … Read more

How to generate .NET 4.0 classes from xsd?

simple enough; just run (at the vs command prompt) xsd your.xsd /classes (which will create your.cs). Note, however, that most of the intrinsic options here haven’t changed much since 2.0 For the options, use xsd /? or see MSDN; for example /enableDataBinding can be useful.

What’s the difference between ViewData and ViewBag?

It uses the C# 4.0 dynamic feature. It achieves the same goal as viewdata and should be avoided in favor of using strongly typed view models (the same way as viewdata should be avoided). So basically it replaces magic strings: ViewData[“Foo”] with magic properties: ViewBag.Foo for which you have no compile time safety. I continue … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)