slovakgirl
12
Q:

c# interfaces

// Interface
interface IAnimal 
{
  void animalSound(); // interface method (does not have a body)
}

// Pig "implements" the IAnimal interface
class Pig : IAnimal 
{
  public void animalSound() 
  {
    // The body of animalSound() is provided here
    Console.WriteLine("The pig says: wee wee");
  }
}

class Program 
{
  static void Main(string[] args) 
  {
    Pig myPig = new Pig();  // Create a Pig object
    myPig.animalSound();
  }
} 
 
3
using System;

namespace Grepper_Docs
{
    public interface IWhatever
    {
        bool doSomething(); //Interface methods don't have bodies or modifiers
    }
  
  	class Program : IWhatever
    {
        static void Main(string[] args)
        {
            var pro = new Program();
            pro.doSomething();
        }

        public bool doSomething() //methods must be public
        {
            return true;
        }
    }
}
2
interface IShape
{
    double X { get; set; }
    double Y { get; set; }
    void Draw();
}

class Square : IShape
{
    private double _mX, _mY;

    public void Draw() { ... }

    public double X 
    { 
        set { _mX = value; }
        get { return _mX; }  
    }

    public double Y 
    {
        set { _mY = value; }
        get { return _mY; }
    }
}
1
public class Car : IEquatable<Car>
{
    public string Make {get; set;}
    public string Model { get; set; }
    public string Year { get; set; }

    // Implementation of IEquatable<T> interface
    public bool Equals(Car car)
    {
        return (this.Make, this.Model, this.Year) ==
            (car.Make, car.Model, car.Year);
    }
}
0

New to Communities?

Join the community