C# Create New T()

Take a look at new Constraint public class MyClass<T> where T : new() { protected T GetObject() { return new T(); } } T could be a class that does not have a default constructor: in this case new T() would be an invalid statement. The new() constraint says that T must have a default … Read more

What does ‘useLegacyV2RuntimeActivationPolicy’ do in the .NET 4 config?

After a bit of time (and more searching), I found this blog entry by Jomo Fisher. One of the recent problems we’ve seen is that, because of the support for side-by-side runtimes, .NET 4.0 has changed the way that it binds to older mixed-mode assemblies. These assemblies are, for example, those that are compiled from … Read more

Cancellation token in Task constructor: why?

Passing a CancellationToken into the Task constructor associates it with the task. Quoting Stephen Toub’s answer from MSDN: This has two primary benefits: If the token has cancellation requested prior to the Task starting to execute, the Task won’t execute. Rather than transitioning to Running, it’ll immediately transition to Canceled. This avoids the costs of … Read more

What is the best way to implement a “timer”? [duplicate]

Use the Timer class. public static void Main() { System.Timers.Timer aTimer = new System.Timers.Timer(); aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent); aTimer.Interval = 5000; aTimer.Enabled = true; Console.WriteLine(“Press \’q\’ to quit the sample.”); while(Console.Read() != ‘q’); } // Specify what you want to happen when the Elapsed event is raised. private static void OnTimedEvent(object source, ElapsedEventArgs e) { … Read more

How can I run PowerShell with the .NET 4 runtime?

The best solution I have found is in the blog post Using Newer Version(s) of .NET with PowerShell. This allows powershell.exe to run with .NET 4 assemblies. Simply modify (or create) $pshome\powershell.exe.config so that it contains the following: <?xml version=”1.0″?> <configuration> <startup useLegacyV2RuntimeActivationPolicy=”true”> <supportedRuntime version=”v4.0.30319″/> <supportedRuntime version=”v2.0.50727″/> </startup> </configuration> Additional, quick setup notes: Locations and … Read more

When should TaskCompletionSource be used?

I mostly use it when only an event based API is available (for example Windows Phone 8 sockets): public Task<Args> SomeApiWrapper() { TaskCompletionSource<Args> tcs = new TaskCompletionSource<Args>(); var obj = new SomeApi(); // will get raised, when the work is done obj.Done += (args) => { // this will notify the caller // of the … Read more

.NET 4.0 has a new GAC, why?

Yes since there are 2 distinct Global Assembly Cache (GAC), you will have to manage each of them individually. In .NET Framework 4.0, the GAC went through a few changes. The GAC was split into two, one for each CLR. The CLR version used for both .NET Framework 2.0 and .NET Framework 3.5 is CLR … Read more

ASP.NET MVC on IIS 7.5 – Error 403.14 Forbidden

ASP.NET 4 was not registered in IIS. Had to run the following command in the command line/run 32bit (x86) Windows %windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe -ir 64bit (x64) Windows %windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_regiis.exe -ir Note from David Murdoch’s comment: That the .net version has changed since this Answer was posted. Check which version of the framework is in the %windir%\Microsoft.NET\Framework64 directory and … Read more

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