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!

Author: Rick Cable / AKA Cyber Abyss

Veteran, Coder, Hacker and Founder of FinditClassifieds.com in 1997. Currently a senior full-stack software developer supporting multiple teams and products at a large healthcare organization.