What is the difference between a class and an interface in C#?

Can you also provide real world examples please!
freezegravityAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Malik1947Commented:
check this out


http://www.csharp-station.com/Tutorials/Lesson07.aspx
http://www.csharp-station.com/Tutorials/Lesson13.aspx

this should explain sufficiently the difference between the two.

0
Éric MoreauSenior .Net ConsultantCommented:
an interface has no code, only methods and properties declarations.

it is used to ensure that multiple classes implements the same interface (set of methods and properties)
0
JimBrandleyCommented:
An interface is like a contract established with consumers of a class. For example, say you create an interface called ISupportNameAndAddress as
public interface ISupportNameAndAddress
{
   string   Address {get; set;}
   string   Name {get; set;}
}

Now you create two simple classes as:
public class Person : ISupportNameAndAddress
{
   private string mAddress = string.Empty;
   private string mName = string.Empty;

   public string Address
   {
      get { return mAddress; }
      set { mAddress = value; }

      // Constructors and other code...
   }
}

public class Company : ISupportNameAndAddress
{
   private string mAddress = string.Empty;
   private string mName = string.Empty;

   public string Address
   {
      get { return mAddress; }
      set { mAddress = value; }

      // Constructors and other code...
   }
}

Now say in some third class, you have a method to print mailing labels. You can:
public void PrintMailingLable( ISupportNameAndAddress addressee )
{
   string name = addressee.Name;
   string address = addressee.Address;
   // Now code to use those values...
}

You can pass that method an instance of a Person or a Company, and it doesn't need to care about which it is. You can accomplish the same thing with base classes, but since in .Net a class cannot derive from multiple classes, that can be limiting.

Jim
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

JimBrandleyCommented:
You may notice I did not add the Name property to either of my classes. That would cause a compiler error to remind me that my classes did not fulfill the contract to provide that property. so, If I add:
   public string Name
   {
      get { return mName; }
      set { mName = value; }
   }

To both classes, it will compile.

Jim
0
freezegravityAuthor Commented:
I think I mostly understand the difference between the two. I will just have to implement the concepts a few times to really get a hang of it.

THANKS!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.