What does “where T : class, new()” mean?

Let’s start with a piece of code;

void Add<T>(T item) where T : class, new();

It’s called a ‘constraint’ on the generic parameter T. It means that T must be a reference type (a class) and that it must have a public default constructor.

That means T can’t be an int, float, double, DateTime or any other struct (value type). It could be a string, or any other custom reference type, as long as it has a default or parameter-less constructor.

For understanding we can break it into two parts;

where T : class

Means that the type T must be a reference type (not a value type).

where T : new()

Means that the type T must have a parameter-less constructor. Having this constraint will allow you to do something like T field = new T(); in your code which you wouldn’t be able to do otherwise.

We then combine the two using a comma to get:

where T : class, new()

Just to clarify, if you don’t have the class clause as part of the where T…, then it is safe to use int, float, double etc

Here is an example;

struct MyStruct { } // structs are value types

class MyClass1 { } // no constructors defined, so the class implicitly has a parameter less one

class MyClass2 // parameter less constructor explicitly defined
{
    public MyClass2() { }
}

class MyClass3 // only non-parameter less constructor defined
{
    public MyClass3(object parameter) { }
}

class MyClass4 // both parameter less & non-parameter less constructors defined
{
    public MyClass4() { }
    public MyClass4(object parameter) { }
}

interface INewable<T>
    where T : new()
{
}

interface INewableReference<T>
    where T : class, new()
{
}

class Checks
{
    INewable<int> cn1; // ALLOWED: has parameter less ctor
    INewable<string> n2; // NOT ALLOWED: no parameter less ctor
    INewable<MyStruct> n3; // ALLOWED: has parameter less ctor
    INewable<MyClass1> n4; // ALLOWED: has parameter less ctor
    INewable<MyClass2> n5; // ALLOWED: has parameter less ctor
    INewable<MyClass3> n6; // NOT ALLOWED: no parameter less ctor
    INewable<MyClass4> n7; // ALLOWED: has parameter less ctor

    INewableReference<int> nr1; // NOT ALLOWED: not a reference type
    INewableReference<string> nr2; // NOT ALLOWED: no parameter less ctor
    INewableReference<MyStruct> nr3; // NOT ALLOWED: not a reference type
    INewableReference<MyClass1> nr4; // ALLOWED: has parameter less ctor
    INewableReference<MyClass2> nr5; // ALLOWED: has parameter less ctor
    INewableReference<MyClass3> nr6; // NOT ALLOWED: no parameter less ctor
    INewableReference<MyClass4> nr7; // ALLOWED: has parameter less ctor
}

Resources

https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/generics/constraints-on-type-parameters

https://stackoverflow.com/questions/4737970/what-does-where-t-class-new-mean

nuget package restore

If you are using private feed then make sure you have installed this;

https://github.com/Microsoft/artifacts-credprovider

I ran this command in powershell (admin mode);

iex ((New-Object System.Net.WebClient).DownloadString('https://raw.githubusercontent.com/microsoft/artifacts-credprovider/master/helpers/installcredprovider.ps1'))

I have to run this command to restore packages;

dotnet restore –interactive

This command will open up a window and ask you to login to Microsoft website with a code. Login to https://microsoft.com/devicelogin and enter the code. This will do some sort of IDE verification. Close VS and reopen. You should be able to see all of your custom and nuget packages restored.

I found this alternative today;

dotnet restore projectName

This will restore the packages.

From within Visual Studio you can use the Package Manager Console to also update the packages. This has the benefit that any PowerShell scripts will be run as part of the update where as using NuGet.exe will not run them. The following command will update all packages in every project to the latest version available from nuget.org.

Update-Package

You can also restrict this down to one project.

Update-Package -Project YourProjectName

If you want to reinstall the packages to the same versions as were previously installed then you can use the -reinstall argument with Update-Package command.

Update-Package -reinstall

You can also restrict this down to one project.

Update-Package -reinstall -Project YourProjectName

The -reinstall option will first uninstall and then install the package back again into a project.

Close and restart VS. Hopefully this will restore the packages.

Another problem might be yellow triangle icon next to package references. To solve this, make sure you are targeting same framework in multiple projects. Here is a discussion about this;

https://stackoverflow.com/questions/20186216/why-do-i-get-a-warning-icon-when-i-add-a-reference-to-an-mef-plugin-project

Resources

https://github.com/dotnet/sdk/issues/10189

https://stackoverflow.com/questions/6876732/how-do-i-get-nuget-to-install-update-all-the-packages-in-the-packages-config

Project targeting multiple framework in .NET

Edit the .csproj file to support the target frameworks; for example change.

<TargetFramework>netcoreapp2.1</TargetFramework>
to:
<TargetFrameworks>netcoreapp2.1;net45</TargetFrameworks>

Make sure that you change the XML element changed from singular to plural (add the “s” to both the open and close tags).

Resources

https://docs.microsoft.com/en-us/nuget/create-packages/multiple-target-frameworks-project-file

Multi-Targeting Frameworks

Clarification on development framework in .NET

This always confuses me. Here is the summary;

  • .NET Framework – the original .NET, the one that ships on Windows and only on Windows; the current (and probably final) version of .NET Framework is 4.8
  • .NET Core – the evolution of .NET, that is not tied to the OS as much, with slightly different feature sets, and where most of the Microsoft .NET effort has been for the last few years; .NET Core 3.1 shipped recently
  • .NET Standard – an API definition (not implementation – akin to an interface) that allows a library to target a range of platforms in a single build, i.e. by targeting .NET Standard 2.0 a library can in theory run equivalently on .NET Core 3 and .NET Framework 4.6.2 (ish…) and others (Mono, Unity, etc), without needing to target each individually
  • .NET 5 – the next version of .NET Core; the naming deliberately emphasizes that there isn’t a two-pronged development future consisting of “Framework” and “Core”, but just one – this one – which isn’t “Core” in the “minimal” sense, but is in fact now a very rich and powerful runtime; .NET 4 was avoided to prevent versioning confusion between .NET 4.* and .NET Framework 4.* (and again, to emphasize that this is the future direction of .NET, including if you are currently on .NET Framework)

Best practices for designing REST API

A reference list of best practices;

https://abdulrwahab.medium.com/api-architecture-best-practices-for-designing-rest-apis-bf907025f5f

https://zonito.medium.com/best-practice-and-cheat-sheet-for-rest-api-design-6a6e12dfa89f

https://medium.com/geekculture/minimal-apis-in-net-6-a-complete-guide-beginners-advanced-fd64f4da07f5

https://levelup.gitconnected.com/5-difficult-skills-that-pay-off-exponentially-in-programming-702a599c636e