when we use interfaces in real time .net example....can any one explain me in detail?

Recommended Answers

All 6 Replies

In theory, every class must have an Interface. The Interface establishes what requests you can make for a particular object.

commented: No, base classes can also define a "signature" for the class. Interfaces are just another way. +0
commented: i agree with sknake +4

Thanks sknake for your comment.

No, base classes can also define a "signature" for the class. Interfaces are just another way. - sknake

What I said:
In theory, every class must have an Interface. Interfaces add behavior to the class. Is it possible to write classes without behavior?

Consider the following statement -
An object has state, behavior, and identity.

Yes but you are getting in to more abstract CS theory than actual implementation of Interfaces in the .NET framework which the post inquired about.

Interfaces add a signature to the class but are not required as your original post indicated. Static classes cannot descend from a base class nor implement interfaces which means that yes, you can write a class without an interface.

Take these compiler errors:

namespace daniweb
{
  public static class TestClass
  {
    public static string Prop1 { get; set; }
  }
  public interface ITestInterface
  {
    void DoWork();
  }
  public static class TestClass2 : TestClass
  {
  }
  public static class TestClass3 : ITestInterface
  {
  }
}

Generates:

Static class 'daniweb.TestClass2' cannot derive from type 'daniweb.TestClass'. Static classes must derive from object.

'daniweb.TestClass3': static classes cannot implement interfaces

Unless you want to argue that the static class descends from object which is true of all types in .NET, I don't think your point stands. In this case the "Interface" of the class is the class definition itself. If that is what you meant them i'm sure you confused someone trying to understand the meaning of an interface in general, aside from the class definition.

i commented to say that i agree with you sknake, but by reflex although agreeing you means disagreeing adatapost, i appeared to add to his reputation positively :) tomorrow for some reason i need to remove that, as well as the next day :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.