Home Fiction Nonfiction About Contact

C# Classes Simplified

C# Classes Simplified

Running 2 or more (classes) by creating and calling them. This is about the simplest way of doing this that I could think of. First, open Visual Studio 2015 and create a new console application and call it “ConsoleApplication1”. Now, at the top of the “Program.cs” page (the page that built and opened by default) you will see your “using” statements. The program will default with 5 "using" statements, but we only need one for this program; the “using System;” statement, so you can remove the others if you like.

Now, let's create a couple of simple "classes" to call in the "Main" class. This is accomplished by right-clicking on “ConsoleApplication1” in the solution explorer and choose “Add” then select “Class”.

For this sample program, do this twice and name them "ExampleP1.cs" and "ExampleP2.cs" respectively. They should look like this example:

Our first class will simply display the "Hello" part of "Hello World". Here is a code snippet of the entire "ExampleP1.cs" page.

Our second class will simply display the "World!" part of "Hello World". Here is a code snippet of the entire "ExampleP2.cs" page.

Note: in this second "class" we added a "Console.ReadKey();" to the method. This is just a way of pausing the program on the screen so you can actually see the results. Otherwise, it will just run and close before you get a chance to see the results. You could also use the more common method "Console.Read();" The difference is that the "Console.Read();" requires you to press the "Enter" key to exit the program, while the "Console.ReadKey();" allows you press "any key" to exit the program. Back to the main page that should be called "Program.cs" (that's the default page in a console application). Here is all the code that should be on the main or "Program.cs" page.

Notice in the "Program" class (just above) we are referencing the "Classes" followed by the "methods". "ExampleP1" and "ExampleP2" are the classes and "Part1()" and "Part2()" are the methods. I know this is very simplistic in nature, but it gives a newbie a chance to understand and work with the basics of calling classes and methods in a simple C# program.
Now, run the program and play around with it at your leisure.


(Published: Sep 2018)