C# Error: The type or namespace name ‘List’ could not be found (are you missing a using directive or an assembly reference?)

OMG are your Kidding Me!

OK, I just spent an hour on this thing while trying to get started on another Tim Corey C# Developer tutorial for creating Microsoft Excel files.

When creating a custom model/object for your C# project and try to use it to build a List that made of that custom object type, you’ll be greeted with an error.

C# Error CS0246: The type or namespace name ‘List<>’ could not be found

Error: The type or namespace name ‘List’ could not be found (are you missing a using directive or an assembly reference?) 

Error seen when creating a List of custom objects without using including the using generics statement.

The missing element is a reference to using System.Collections.Generic

Code Example

using OfficeOpenXml;
using System;
using System.IO;
using System.Collections.Generic;


namespace ExcelDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //Set the EPPlus license context to nonCommercial so we can play with it :-)
            ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
            var file = new FileInfo(@"c:\scripts\demo.xlsx");
        }

     static List<PersonModel> GetSetupData() 
        {
            List<PersonModel> output = new()
            {
                new() { Id = 1, FirstName= "rick", LastName = "cable" },
                new() { Id = 2, FirstName="bob",LastName= "marley" },
                new() { Id = 3, FirstName="willie",LastName= "nelson" }
            };

            return output;
        }
    }
}

References

As always, give credit where credit is due. In this case, a big thank you to David Morton for the post on the Microsoft Visual Studio forums.

I hope this blog post helps somebody!

Cyber Abyss!

C# Generics: Digesting Tim Corey’s C# Generics Video

C# Generics: Digesting Tim Corey’s C# Generics Video

Tonight, I’m completing a learning session on C# Generics. I’m using Tim Corey’s YouTube video on C# Generics as my tutorial.

You can get all the files to go along with Tim Corey’s C# Generics video at Time’s web site for the cost of providing Tim with your email address for his email list. You can unsubscribe anytime.

Below is Corey’s Video. Watch it, then if you’re still interested. My personal training notes will be shared below. It might help you if you’re feeling stuck or want to cement an idea in your own mind.

Tim Corey’s C# Generics Video