AddTransient, AddScoped and AddSingleton Services Differences

TL;DR Transient objects are always different; a new instance is provided to every controller and every service. Scoped objects are the same within a request, but different across different requests. Singleton objects are the same for every object and every request. For more clarification, this example from .NET documentation shows the difference: To demonstrate the … Read more

Calling the base constructor in C#

Modify your constructor to the following so that it calls the base class constructor properly: public class MyExceptionClass : Exception { public MyExceptionClass(string message, string extrainfo) : base(message) { //other stuff here } } Note that a constructor is not something that you can call anytime within a method. That’s the reason you’re getting errors … Read more

Type Checking: typeof, GetType, or is?

All are different. typeof takes a type name (which you specify at compile time). GetType gets the runtime type of an instance. is returns true if an instance is in the inheritance tree. Example class Animal { } class Dog : Animal { } void PrintTypes(Animal a) { Console.WriteLine(a.GetType() == typeof(Animal)); // false Console.WriteLine(a is … Read more

Is there a reason for C#’s reuse of the variable in a foreach?

The compiler declares the variable in a way that makes it highly prone to an error that is often difficult to find and debug, while producing no perceivable benefits. Your criticism is entirely justified. I discuss this problem in detail here: Closing over the loop variable considered harmful Is there something you can do with … Read more

What do two question marks together mean in C#?

It’s the null coalescing operator, and quite like the ternary (immediate-if) operator. See also ?? Operator – MSDN. FormsAuth = formsAuth ?? new FormsAuthenticationWrapper(); expands to: FormsAuth = formsAuth != null ? formsAuth : new FormsAuthenticationWrapper(); which further expands to: if(formsAuth != null) FormsAuth = formsAuth; else FormsAuth = new FormsAuthenticationWrapper(); In English, it means … Read more

How do I remedy “The breakpoint will not currently be hit. No symbols have been loaded for this document.” warning?

Start debugging, as soon as you’ve arrived at a breakpoint or used Debug > Break All, use Debug > Windows > Modules. You’ll see a list of all the assemblies that are loaded into the process. Locate the one you want to get debug info for. Right-click it and select Symbol Load Information. You’ll get … Read more

How do I create an Excel (.XLS and .XLSX) file in C# without installing Microsoft Office?

You can use a library called ExcelLibrary. It’s a free, open source library posted on Google Code: ExcelLibrary This looks to be a port of the PHP ExcelWriter that you mentioned above. It will not write to the new .xlsx format yet, but they are working on adding that functionality in. It’s very simple, small … Read more

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