C# static 🚫
HTML-код
- Опубликовано: 2 июл 2021
- C# static modifier tutorial example explained
#C# #static #modifier
using System;
namespace MyFirstProgram
{
class Program
{
static void Main(string[] args)
{
// static = modifier to declare a static member, which belongs to the class itself
// rather than to any specific object
Car car1 = new Car("Mustang");
Car car2 = new Car("Corvette");
Car car3 = new Car("Lambo");
Console.WriteLine(Car.numberOfCars);
Car.StartRace();
Console.ReadKey();
}
}
class Car
{
String model;
public static int numberOfCars;
public Car(String model)
{
this.model = model;
numberOfCars++;
}
public static void StartRace()
{
Console.WriteLine("The race has begun!");
}
}
} - Наука
using System;
namespace MyFirstProgram
{
class Program
{
static void Main(string[] args)
{
// static = modifier to declare a static member, which belongs to the class itself
// rather than to any specific object
Car car1 = new Car("Mustang");
Car car2 = new Car("Corvette");
Car car3 = new Car("Lambo");
Console.WriteLine(Car.numberOfCars);
Car.StartRace();
Console.ReadKey();
}
}
class Car
{
String model;
public static int numberOfCars;
public Car(String model)
{
this.model = model;
numberOfCars++;
}
public static void StartRace()
{
Console.WriteLine("The race has begun!");
}
}
}
My bro You're a G, thanks for the vid
I'm a beginner programmer. I feel like you have this innate ability to smash through all the knowledge that you have, and distill it all into what even a beginner can understand.
It's so clear and concise. You just leave all that extra knowledge by the side and it keeps you from overexplaining. You're amazing.
9 months later where's your knowledge at :D
@@khajiit5556 "Explain" doesn't mean the knowledge he poured into his student's head will stay there forever you get an explanation if u make use of it or not is up to you
Oh and btw, if we use your logic to memories you have does it mean you didn't live through your early years or what?
@@ch1nc2yk39 bro what are you saying😂
Best explanation of how we should use statics I have found.
I have been programming for years at a somewhat base level but I have had multiple people explain static to me and this video is the first time I've actually fully grasped the use of it and the power behind it. Thanks for the short but knowledge filled video.
I've been finding it pretty hard to understand concepts because every other resource i've used overexplains everything, you just make it clear and concise. I'm finally starting to understand, I didn't know I can learn the basics of static members in just 5 minutes. Thanks Bro Code!
Bro, you are a really brilliant teacher! For 10 years I have been struggling with this static thing. Now I understood it in 5 minutes. That is amazing!!! Thank you!
Bro thank you so much. THIS IS A HUGE HELP.
Went through like 5 videos explaining this topic and yours was the only one that made sense. Good stuff!
Awesome, you made this sound way more simpler compared to stackoverflow (:
and compared to my OOP teacher 😅
You are fantastic. Please never abandon us, keep on creating videos
You should have seen me cheering up when you finally made this whole thing clear. Now I clearly understand what a static member means in a class. Thank you so much.
Amazing explanation! All of your examples are really good!
No one explained this easier than you bro. Thank you so much!
Bro on god as a hobbyist programmer I have not understood what the fuck static is for for years and would just turn it on and off depending on if the compiler is yelling at me or not. And you just managed to explain exactly what it's for and when it would be necessary in under two minutes so clearly that I now perfectly understand when and how to use it. You're a god among men.
Thanks bro.
Been following you since you had only 2k subscribers
That was easy
Love all of ur videos they answer the questionz that kept bothering me for so long . U earned a sub
Your explanation is amazing! Thank you!!
where have you been all my life
These videos are so easy to understand
Thanks Bro
Wow, so well explained. Genius! 🙏🏻🙏🏻🙏🏻 Thanks a lot! :D
This was GREAT! Thank you
Very nice explanation for someone who's learning C# :)
Best video about static i`ve watched. Thanks Bro.
Excellent explanation!
Thank you i was trying to count within the class and could not figure it out this helps a ton!
AWESOME explanation!! Thank you!!
Oh yeah, thanks for the video Bro.
Awesome videos! Keep it up!
Very clear explanation, thanks!
What a great video. Thanks
Bro, you explained this so well!
This was so useful! Thanks man!
Bro your explanation is superior
My college teacher just went through the comment bit explaining it but then didnt explain that whereas you have explained the explanation which i now understand, thanks
He's truly a Chad when it comes to explaining stuff.
great tutorial
Thanks so much!
Thank you,and good vídeo
Great that you write the code to your video .
these tutorials are god_tier
The definitions you use are up to the point🏹
So easy to understand. thank you.
Thank you bro!
nice explanation bro
awesome tut!!! what's your vs theme btw?
Finally a good explanation
very clear thank you!
Concise and great.
the best explanation thank you so much
good explaination
you are such a bro, bro.
That's great, now, I just know how to use static modifier, thanks..
nice explanation
amazing
Thanks Bro!
luv u bro
Thanks bruv
thanks!
Thank you
Thanks
Just for me to Refresh memory, thanks
It was so clear
nice!
this code bro guy is really cool
Ive spent forever trying to understand why you would want to use static and it finally clicked.
noice
brabo
Yessss
thanks for the video, really nice! I tried to use your code in Visual Studio and get error message: Compiler Error CS5001 "
Program does not contain a static 'Main' method suitable for an entry point". Hence i cannot run it. Anything i can do to make it run and close this error message? Thanks !
i love u
❤
Question: Is there any difference between using the static modifier vs the abstract modifier for a class?
Yes. Abstract classes act as blueprints for other classes to inherit from while static classes serve as utility fixed classes.
Quick example for static class: If you want to build a simple calculator, you can create a static class named Operations and have 4 static methods (add, subtract, division, multiply), each method will perform an operation with int a & b. To call it, simply put Operations.Add(1,2) in your main program. Other classes won't be able to modify the method and will not be able to create objects from this class.
Quick example for abstract class: You want animals to make a sound. You will create an abstract class named Animals and create an abstract method MakeSound(), You're now preventing people from creating an object from Animal, which by itself is not an animal. Now you create a Dog class that inherits from Animal. Now you can override the MakeSound method to console write "bark". If you want to hear the bark, you need to create a Dog object and then call the method:
class Dog : Animal
Dog dog = new Dog();
dog.MakeSound(Console.WriteLine("Bark");
I hope this helps you or anyone else struggling to understand access modifiers. It's very important to know them well for professional success.
Super @@jorgeandrespalma2134
bro do you like unity game engine development
Bro please explain goto statement in cpp
true American programmer🤣🤣
lesson check😇
:)
Random comment years later
the only bro we need is you not pewdiepie
Random comnet
Except, a car class shouldn't be keeping track of the number of cars there are and when the race has started.
Random comment
meow (as you asked for a random comment) :)
A random comment down below
Random comment down below
random comment down below
Thanks Bro!