UnknownUser
UnknownUser

Reputation: 133

C# 'Default1': member names cannot be the same as their enclosing type

I'm getting this error and sure how to fix it, I'm trying to create a loader class that inherits from the Default1, then to use its in the method.

namespace MyLevel
{
    class Default1
    {
        public interface Default1
        {
            IEnumerable<DefaultCore> GetDefaultValues();
        }
    }
}

Any help appreciated

Upvotes: 0

Views: 870

Answers (4)

James Johnson
James Johnson

Reputation: 46067

If you want the class to inherit the interface, it needs to be outside of the class. Secondly, utilize standard naming conventions and prefix the interface name with an I ( e.g. IDefault, ICloneable, IDisposable, etc.):

namespace MyLevel
{
    public class Default : IDefault
    {

    }

    public interface IDefault
    {
        IEnumerable<DefaultCore> GetDefaultValues();            
    }
}

Upvotes: 1

Michael Edenfield
Michael Edenfield

Reputation: 28338

I think you are confused here about how to define and/or implement interfaces.

What you have there is a class, named Default1, which is defining an internal, nested interface named Default1. There are three problems with this:

  1. You can't do that; the names cannot be the same because it creates possible ambiguity with the names. The error message was pretty clear here. (As others have stated, interfaces are usually called ISomething, which resolves the name conflict.

  2. That interface is defined in a strange way; it is nested inside an internal (thanks @FishBasketGordo for reminding me!) class. That means it's only visible inside the assembly where you defined it, and it has to be referenced as Default1.IDefault (assuming you fixed the name).

  3. Nothing is actually implementing your interface, so it is not being used anywhere.

If you want to define an interface, you don't need a class:

namespace MyLevel
{
    public interface IDefault
    {
        IEnumerable<DefaultCore> GetDefaultValues();
    }
}

If you want your class to implement that interface:

namespace MyLevel
{
    public class Default1 : IDefault
    {
        public IEnumerable<DefaultCore> GetDefaultValues() { }
    }
}

Upvotes: 3

FishBasketGordo
FishBasketGordo

Reputation: 23142

Because you've defined the interface within the class, it is a member of the class, and a class cannot have two members with the same name nor can it have a member with the same name as the class itself. That's what the error message is saying.

Thus, change the name of either Default1 (the class) or Default1 (the interface).

Most interfaces are prefixed with a capital I by convention, so IDefault1 would be a good choice for the interface. Also, unrelatedly, why are you defining an interface within a class?

Upvotes: 1

PRASHANT P
PRASHANT P

Reputation: 1547

Your code is not evaluate.

Interface is (of normal) to start with the I

not requirement for the build, yet common as the LAW

just go IDefault !

PRASHANT :)

Upvotes: -1

Related Questions