C
C#2mo 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
Anton2mo ago
polymorphism
Pobiega
Pobiega2mo 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
Anton2mo 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
Pobiega2mo 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
Anton2mo ago
you can do 99% of things without them
Pobiega
Pobiega2mo ago
so @CloudFox can you clarify?
Anton
Anton2mo ago
yeah you're right. not static, just methods
CloudFox
CloudFoxOP2mo ago
I don't know what the interface does
Anton
Anton2mo ago
it lets you call a method without referring to it directly
Pobiega
Pobiega2mo 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
Anton2mo ago
and then pass an object that has a method you want to get called
Pobiega
Pobiega2mo 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
CloudFoxOP2mo 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