.Net – Compilation Error Message – “error CS0246: The type or namespace name ‘[interface]’ could not be found (are you missing a using directive or an assembly reference?)”

Background

Continuing to play around with .Net C#.

Specifically, Generics.

Ran into an error I could not get beyond.

Error

Error Message

error CS0246: The type or namespace name '[interface]<>' could not be found (are you missing a using directive or an assembly reference?)

Code

Error Message


var incr = new Increment<ClassA>();

 

TroubleShooting

Add Namespace

Code

Preface usage by indicating the code uses classes in the System.Collections.Generic namespace.


using System.Collections.Generic;

 

Error

Compiled and got an error that reads:-


error CS0144: Cannot create an instance of the abstract class or interface 'Increment<ClassA>'

Remediation

Add Third-Party Namespaces

Code


using <thirdPartyPackage>;

 

 

Sanitation

Full Name

Revised Code

Went back and added “using System.Collections.Generic”.

Code


using System.Collections.Generic;

Compiled and ended with a new error message.

Error

Specifically:-


error CS0104: 'Increment<>' is an ambiguous reference between 'System.Collections.Generic.Increment<T>' and 'thirdPartyPackage.Increment<T>' 

To fix the ambiguity error, please prefix the interface name with the namespace.

Code


var incr = new <namespace>.Increment<ClassA>();

 

Summary

Quick Points

  1. As you learn things a bit and use things such as Generics, the language parser sometimes stumbles a bit.
  2. One quick fix is to quickly add in “using System.Collections.Generic;
  3. Unfortunately in my particular case got an error that reads:-
    • CS0144: Cannot create an instance of the abstract class or interface ‘Increment<ClassA>’
  4. Removed “using System.Collections.Generic;
  5. And, added the specific third party namespace that defines the applicable interface

Though object-oriented programming is very helpful with avoiding name collision, one is not completely out of the woods.

 

Disclaimer

Intentionally I obtused the name of the actual package that I was exploring.

Also, the actual method name.

When writing a post, one can come at it from different angles.

I did not want to draw focus away from the error message.

The error message itself was a bit more interesting than the package itself.

 

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s