C# interfaces 🐟

preview_player
Показать описание
C# interfaces tutorial example explained

#C# #interfaces #interface

using System;

namespace MyFirstProgram
{
class Program
{
static void Main(string[] args)
{
// interface = defines a "contract" that all the classes inheriting from should follow

// An interface declares "what a class should have"
// An inheriting class defines "how it should do it"

// benefits = security + multiple inheritance + "plug-and-play"

Rabbit rabbit = new Rabbit();
Hawk hawk = new Hawk();
Fish fish = new Fish();

rabbit.Flee();
hawk.Hunt();
fish.Flee();
fish.Hunt();

Console.ReadKey();
}
interface IPrey
{
void Flee();
}
interface IPredator
{
void Hunt();
}
class Rabbit : IPrey
{
public void Flee()
{
Console.WriteLine("The rabbit runs away!");
}
}
class Hawk : IPredator
{
public void Hunt()
{
Console.WriteLine("The hawk is searching for food!");
}
}
class Fish : IPrey, IPredator
{
public void Flee()
{
Console.WriteLine("The fish swims away!");
}
public void Hunt()
{
Console.WriteLine("The fish is searching for smaller fish!");
}
}
}
}
Рекомендации по теме
Комментарии
Автор

using System;

namespace MyFirstProgram
{
class Program
{
static void Main(string[] args)
{
// interface = defines a "contract" that all the classes inheriting from should follow

// An interface declares "what a class should have"
// An inheriting class defines "how it should do it"

// Benefit = security + multiple inheritance + "plug-and-play"

Rabbit rabbit = new Rabbit();
Hawk hawk = new Hawk();
Fish fish = new Fish();

rabbit.Flee();
hawk.Hunt();
fish.Flee();
fish.Hunt();

Console.ReadKey();
}
interface IPrey
{
void Flee();
}
interface IPredator
{
void Hunt();
}
class Rabbit : IPrey
{
public void Flee()
{
Console.WriteLine("The rabbit runs away!");
}
}
class Hawk : IPredator
{
public void Hunt()
{
Console.WriteLine("The hawk is searching for food!");
}
}
class Fish : IPrey, IPredator
{
public void Flee()
{
Console.WriteLine("The fish swims away!");
}
public void Hunt()
{
Console.WriteLine("The fish is searching for smaller fish!");
}
}
}
}

BroCodez
Автор

Interface declares: "What a class should have"

An inheriting class defines: "How it should do it"
Chef's kiss of an explanation, thanks sir

crazyskater
Автор

Your explanations are one of the clearest and easiest to remember ones I've ever come across. Great job!

BlueHat
Автор

My teacher has spent hours confusing this for me. This was so clear

Obalanserad
Автор

I have seen other videos, and this is the best explanation i've seen so far. Thanks a lot. I've subscribed.

jacobsonokoro
Автор

Zero BS, straight to the point. After watching your video I inmediately understood how to refactor my existing programs using Interfaces. Thank you very much!

rhr-pw
Автор

getting into programing for game development as I have always been fascinated by it and your vids are the best I have found. You explain it in such a way that actually helps me learn. Most coding videos on YouTube are so hard to listen to as they don't know how to communicate their skills properly. But I thank you for doing such a good job with it as your videos help me a lot.

sgn
Автор

Your ability to explain such complicated things so simply is simply amazing to me.
Thank you for a great channel and quality content!

liorlior
Автор

in just 5 min whole concept is clear...thanks a lot..

Cherry-jcbo
Автор

I have spent YEARS trying to understand this. today, i fully understood this in 5 minutes . WOW

dzulanimashau
Автор

In an hour i'm writing an exam based on this. 10 minutes ago I knew shit about interfaces, now I know all I need. Huge thanks goes to this man! 🙏🏿

mojzi
Автор

This helped me so much I was so confused having to do this at my internship and I got the hang of it in 5 minutes because you explained this so well! Thank thank you

gaminggoddessaria
Автор

After days of studying and going through lecture material on this topic, I was still clueless until your 5' tutorial came to the rescue. 🙏

Delmas.
Автор

2:16 Rabbit... rabbit... equals new... Rabbit. Made me lol. Excellent video.

jayanders
Автор

My guy just made an intermediate programming concept seem look like a beginner programming concept. Thank you Mr.Chad.

mysteryultragames
Автор

Most simple explanation I have seen on interfaces. Thank you!

despahotaru
Автор

Dude this is one of the best explanations of the interface of c#

blasterxt
Автор

After years, your tutorials still the best

IPrey for you 🙏

xejdikmiecik
Автор

Spent a couple of years learning C# online through YouTube videos, I learned alot to the point where I created a console program to make pizzas from scratch. The program I wrote allowed me to input keys to the console that allowed option selection. I learned that through Michael Hadley's Intro to Programming tutorials.

However I struggled like a muddabucka with a few things to this day. Interfaces being one them. Today I am on Unity exploring real game design, and their lesson courses are amazing. All the C# know-how I learned proved worth it, because I am flying through the courses - because Unity's documentation doe alot of work for you. But there was a course that used IEnumerators in Unity.

Sure I understood what the IEnumerator as for - but I still never understood Interfaces themselves. Now I do thanks to your video. And i's such a clear and easy concept i feel so dumb tonot have grasped them before.

But thanks man! ... I mean... Bro.

jhappysemall
Автор

God bless people like you who distill this information down to what is necessary without waffling on.

gowthamkrishna