• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 294
  • Last Modified:

Interfaces in C#?

Hi,

i am a JAVA developer and learning now C#...

Can someone tell me if Interfaces also in C# available? If yes how i can use them?

Kind Regards Stefan
0
woigl
Asked:
woigl
1 Solution
 
c_myersCommented:
Yes. C# has Interfaces. They behave the same as in Java with two exceptions:
1.) C# Interfaces cannot have fields/constants like in Java.
2.) C# Interfaces do not allow for accessibility declarations (i.e. no 'public', 'private', 'protected', etc) in the interface.

Everything else is essentially the same.

Here are the official docs on it:
http://msdn2.microsoft.com/en-US/library/87d83y5b(VS.80).aspx

To summarize:

To create an interface:

public interface IAnimal
{
     void Eat();
     void Sleep();  
}

To implement an interface:

public class Dog : IAnimal
{
     public void Eat()
     {
         // Do some eating here
     }

     // Explicit interface definition, does not show up on Dog's Intellisense unless you cast it explicitly to an IAnimal reference
     void IAnimal.Sleep()
     {
          // Sleepy stuff here
     }
}
0

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now