This article is about C# Simple tutorial for beginners , This is the first 9 piece . Thank you for watching. , Remember to follow me , There are more tutorial articles to follow , thank you .
The environment of this paper is Visual Studio 2019.
One 、 Turn the real world into a class
Class is a code description of the real world , In other words, encapsulation . This is my own exclusive explanation , Copy what you don't have online .
Through encapsulation , We can use the method 、 Properties, etc , To describe a real world .
Like the family in life , We can express it as having a method “ Family member list ”, There are properties “ Family income ”, wait .
Class in C# Use in class To define , Its properties and methods, etc , We call them members of this class ：
- These properties are used to describe the data that the class will hold .
- These methods tell you what you can do with the data .
Two 、 What is an object
An object is simply an instantiation of a class , stay C# in , By keyword new You can create an object .
When you create an object , Will use constructors , There is usually a default parameterless constructor . A constructor is a special member of a class .
3、 ... and 、 stay VS How to create a class
open Visual Studio After creating a new project , You can right-click -> add to -> Create a class by creating a new item .
Pictured above , You can create a new class .
Four 、 How to write your own code logic in a class
After you have a class , With “ family ” For example , Take a look at how to express the real world , Write it to... In the code .
Our family may need a list of members , It could be a way , Of course, you can use attributes ：
And then our family , There may also be the attribute of family income ：
And then our family , Maybe it has its own name , It could be a static property ：
Last , We need to instantiate it ：
Have a good meal .