C
C#3w ago
CloudFox

What is the use of Interface

I'm wondering what is the difference between referencing "void" directly from another file and calling it with "Interface"? I'm a newbie to C#, and a lot of cross-file code calls are used directly with a new definition of that file name, such as "public Player_Data PD"
13 Replies
Anton
Anton3w ago
polymorphism
Pobiega
Pobiega3w ago
Can you clarify exactly what the question is here? Are you asking how to properly call methods from another file, or do you want to know what the interface keyword is used for?
Anton
Anton3w ago
they are saying they have static methods I assume they were told to try interfaces interfaces are for when a function has to do different things depending on the context kind of you're fine with regular methods you don't need interfaces, unless you know you need them
Pobiega
Pobiega3w ago
I don't think thats what they are saying at all.
used directly with a new definition of that file name,
sounds more like new Example().MethodName(); to me, with a potential variable in there too
Anton
Anton3w ago
you can do 99% of things without them
Pobiega
Pobiega3w ago
so @CloudFox can you clarify?
Anton
Anton3w ago
yeah you're right. not static, just methods
CloudFox
CloudFox3w ago
I don't know what the interface does
Anton
Anton3w ago
it lets you call a method without referring to it directly
Pobiega
Pobiega3w ago
its a way to declare a contract that implementing classes must adhere to. This allows you to call methods without knowing the concrete implementation.
Anton
Anton3w ago
and then pass an object that has a method you want to get called
Pobiega
Pobiega3w ago
example:
interface IAnimal
{
string Name { get; set; }
string Cry();
}

class Dog : IAnimal
{
public string Name { get; set; }
public string Cry()
{
return "Woof!";
}
}

class Cat : IAnimal
{
public string Name { get; set; }
public string Cry()
{
return "Meow!";
}
}

static void Main(string[] args)
{
var animals = new List<IAnimal>();

animals.Add(new Dog { Name = "Robby" });
animals.Add(new Dog { Name = "Fify" });
animals.Add(new Cat { Name = "Mimy" });

PrintAnimals(animals);

Console.ReadKey();
}

private static void PrintAnimals(IEnumerable<IAnimal> animals)
{
foreach (IAnimal animal in animals) {
Console.WriteLine("Here is {0}: {1}", animal.Name, animal.Cry());
}
}
interface IAnimal
{
string Name { get; set; }
string Cry();
}

class Dog : IAnimal
{
public string Name { get; set; }
public string Cry()
{
return "Woof!";
}
}

class Cat : IAnimal
{
public string Name { get; set; }
public string Cry()
{
return "Meow!";
}
}

static void Main(string[] args)
{
var animals = new List<IAnimal>();

animals.Add(new Dog { Name = "Robby" });
animals.Add(new Dog { Name = "Fify" });
animals.Add(new Cat { Name = "Mimy" });

PrintAnimals(animals);

Console.ReadKey();
}

private static void PrintAnimals(IEnumerable<IAnimal> animals)
{
foreach (IAnimal animal in animals) {
Console.WriteLine("Here is {0}: {1}", animal.Name, animal.Cry());
}
}
CloudFox
CloudFox3w ago
I seem to understand something, I'll give it a try, thank you very much!
Want results from more Discord servers?
Add your server