Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a code sample I don't understand:
C#
class LinkedList<T> : IEnumerable<T>
{
    public IEnumerator<T> GetEnumerator()
    {
    }

    IEnumerator IEnumerable.GetEnumerator()
    {

    }
}


1. IEnumerable.GetEnumerator(), what's the usage put the interface in front of the method name?

2. why IEnumerable.GetEnumerator() cannot have a modifier, if I change this to :
public IEnumerator IEnumerable.GetEnumerator(), the compiler says : "The modifier 'public' is not valid for this item"
Posted

An interface is put infront of the name to tell the compiler that this class should implement methods in that interface.
Interfaces in C# (For Beginners)[^]

An interface is a contract to the outside world and its members do not have access modifiers.
 
Share this answer
 
"1."
The interface's name in front of the interface's method name, tells the compiler it's an explicit method implementation. Thus, this method can only be called from an instance of this interface.

"2."
This kind of declaration, already defines who can call the method, i.e., any variable that's a reference to this specific interface, for an object of that implements that interface.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900