Enough small talk. Start a new console application called "Classes".
Create a new class by clicking on Project -> Add Class.
Select the template "Class", fill in the name "View" and press Add.
A new file has been created in your project and it's called "View.cs" which can be seen in your solution explorer. The file has been opened and you can see the contents in your text editor.
Add the following code to your "View" class:
public View()"public View()" is not a method, it's a constructor. It must have the same name as the class and it mustn't have a return data type. The constructor is called when the class has been instantiated .
Switch back to the "Program.cs" file and create an instance of the class by typing:
View view = new View();Now the class has been instantiated and the constructor will be called when running your application. Note that the instance "view" of the class "View" could've easily been called anything. The instance "view" doesn't need to be the same name as the class in lowercase. Press F5 on your keyboard to run your application now and "I'm alive!" should be shown on your screen.
Go to your class "View" and add the following method:
public void ShowOutput(string text)The access modifier needs to be "public", because you're going to call the "ShowOutput" method from the class where you instantiated the "View" class.
Back to your "Program" class again. To call the "ShowOutput" method in the "View" class you will have to type the following below "View view = new View();":
view.ShowOutput("Hello World!");Press F5 on your keyboard and you should see "Hello World!" on your screen after pressing enter once, because your constructor has a "Console.ReadLine();" and your application executes that first.
Use classes to give your application a logical structure. Try to divide classes in how you would do in real life. If you create a game tic tac toe and you want to check if someone made a valid move, then for example create a class called referee.
Source Code Classes Project