C# objects 🧍♂️
Вставка
- Опубліковано 15 жов 2024
- C# object oriented programming tutorial example explained
#C# #objects #classes
using System;
namespace MyFirstProgram
{
class Program
{
static void Main(string[] args)
{
// object = An instance of a class
// A class can be used as a blueprint to create objects (OOP)
// objects can have fields & methods (characteristics & actions)
Human human1 = new Human();
Human human2 = new Human();
human1.name = "Rick";
human1.age = 65;
human2.name = "Morty";
human2.age = 16;
human1.Eat();
human1.Sleep();
human2.Eat();
human2.Sleep();
Console.ReadKey();
}
}
class Human
{
public String name;
public int age;
public void Eat()
{
Console.WriteLine(name + " is eating");
}
public void Sleep()
{
Console.WriteLine(name + " is sleeping");
}
}
}
using System;
namespace MyFirstProgram
{
class Program
{
static void Main(string[] args)
{
// object = An instance of a class
// A class can be used as a blueprint to create objects (OOP)
// objects can have fields & methods (characteristics & actions)
Human human1 = new Human();
Human human2 = new Human();
human1.name = "Rick";
human1.age = 65;
human2.name = "Morty";
human2.age = 16;
human1.Eat();
human1.Sleep();
human2.Eat();
human2.Sleep();
Console.ReadKey();
}
}
class Human
{
public String name;
public int age;
public void Eat()
{
Console.WriteLine(name + " is eating");
}
public void Sleep()
{
Console.WriteLine(name + " is sleeping");
}
}
}
Radical!
Thanks for pasting the code as well. Question- How come when you create the 'name' field within the Human class, the String has 'S' capitalized? I guess I'll find out later
Omg you're awesome! I struggled with OOP for 2 weeks in class and you just made this so simple. Thank you!
I couldn't agree more
Awesome stuff! I’m learning C# through a book called C# players guide. It’s a very good book but your content is really solidifying some of the complex key terms
@Bro, Your Teaching skills and the examples you use are phenomenal. I've learned from nothing to something, All thanks to these tutorials.
Great video! Very easy to grasp and is easy to understand the concepts of a class. Appreciate this!
Nice video bro!! you help to understand in a good way how objects works!!
My teacher just send our class a 30 minutes video and your videos just saved me 20 minutes. Well deserved for a new subscriber
Perfect. Very well explained with a good example.
simple but effective teaching!
Bro are awesome. Been trying to figure OOP thinking it's rocket science but you have made it where even I can understand it. Actually I got a degree in rocket science but that doesn't make me a rocket scientist.
Bro you are the best im trying to understand what is object for hours and you just made it clear, thak you so much!
You explained this so well thank you!
Very straightforward and relatable 👍
wubba lubba dub dub!
Jhinga lala hoo hoo
great video! easy to understand bro
Great breakdown, thanks bro.
can you create tutorial vids like this using WinUI 2 or 3 UI frameworks?
If I let coding make me nervous, then I can’t get schwifty
Thanks for the tutorial, but is there a faster/more intuitive way to define an object with all of its properties? It seems like defining many objects with many different properties would be very time consuming. I'm more familiar with Python, where it's as easy as calling the class with all of the desired values for the properties.
Nevermind! The answer is in the next video: ua-cam.com/video/cAhh6pYkHPQ/v-deo.html
Simply great! Thanks a lot!
Great explanation, thanks again Bro
Thank's for tutorials.
Love you bro, youre the best
Evereyone's first project: "Hello world"
My first project: Creating a human being from scratch
clear and smooth af, thank you
Best channel out there
YOU ARE JUST WOWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW
Thanks for the video Bro.
you're the best Bro!
THANKS FOR THE KNOWLEDGE BRO
Normally I put a random comment down below but why is it considered insecure to make them public? what would be the preferred solution?
Im assuming having a second page for classes, and you can make them public on that page and just call them in to where you are woeking.
Maybe a bit late to reply. In this case it doesn't matter, but usually fields should never be set to public, because they could be modified from anywhere in the program without the possibility for a validation of the newly assigned value. Take age for example: if you set it to public, anyone can just set it to whatever... 20, 60, 0, -123456. And then if your application relies on the value of age, it will have terrible consequences for the logic. Where as if you make the age private and also make 2 methods - void SetAge(int age) and int GetAge(), you can then add all the validation you want inside the SetAge method and throw exceptions if someone tries to add ages that make no sense, and use GetAge to actually get the value. Later on in this series you will learn more about this, but this is called Encapsulation (basically protection of data) and in C# you don't even need to do it with methods, but you'll learn to do it with properties (which pretty much mimic/replace those two methods i mentioned above). And as an additional info - fields are usually used for the internal logic of classes, whereas properties are exposed with public to the rest of the application so that people can use them instead of the fields.
Can you do a tutorial in C?
thank you so much for this
very useful!
Thanks.
bladah bang bro yeyeyeyye!
Thank you
Thank you Bro!!!
Thanks Bro!
random comment 😂😂 tbh love your videos
if i want ro have my veriables private what do i do then?
thanks bro
Thank Bro!
Thanks man!
nice!
the Goat
Großartig!!
actual gigachad coder
Wow!
done
Bro code awesome
Nooooice
I promote you to Dude! Thanks! Dude!!!🤣😂🤣😂
👍🏻👍🏻👍🏻👍🏻👍🏻💯💯💯💯😍😍😍
w
System this is a random coment to help Brintey Stasy
👍
Aww, geez
"Wubba Lubba dub dub"
noice
wubba lubba dub dub!!! 85th here
wubba lubba dubba
wubba lubba dub dub
Random comment = new Random(10,12);
Here it is your random comment. Thank you for simplifying concepts and making everything way easier!
interaction
Wubba Lubba Dub Dub!!!
lesson check😇
Watching in 2024
🗿
suup
357
random comment down below
Random comment
random
random comment
Ln
Thanks bro helped a lot. 🫡
Console.WriteLine("Random comment");
wubba lubba dub dub!
wubba lubba dub dub
u forgot the "!" at the end
random
Wubba libba dub dub!