timmyg
timmyg

Reputation: 1055

C# equivalent of Java 'implements' keyword?

In Java if you were to have the statement:

public class MyClass implements LargerClass {

Would you be extending the LargerClass with more methods?

What would be the equivalent of this class definition in C#?

I ask because I am not very familiar with Java and am currently converting some Java code to C# code and this one is giving me some trouble.

Thanks in advance.

Upvotes: 26

Views: 30427

Answers (3)

Rob
Rob

Reputation: 48379

public class MyClass implements LargerClass

In Java, this declares that MyClass implements the interface LargerClass; that is, sets out implementations for the behaviours defined in LargerClass.

To inherit from another class in Java, use extends, e.g.

public class MyClass extends LargerClass

The C# equivalent, in both cases, is specified as

public class MyClass : LargerClass

Since this syntax doesn't make it clear whether or not LargerClass is an interface or another class being inherited, you'll find C#/.NET developers adopt the convention that interface names are prefixed with uppercase "I", e.g. IEnumerable.

Upvotes: 76

TheSmurf
TheSmurf

Reputation: 15568

public class MyClass : LargerClass {...}

Upvotes: 3

David Morton
David Morton

Reputation: 16505

public class MyClass : LargerClass
{

}

Upvotes: 8

Related Questions