Program Structure
The least amount of code required to make a console app in C# presented below.
namespace ConsoleApp1
{
class Program
{
static void Main()
{
}
}
}
Some rules applied here:
- All code contained in
class
. - Everything between those two curly braces is considered part of the class.
- Program can have any number of classes and filed separately.
- If a file only contains one class, you'll want to name the file same as the class name.
- The .cs at the end of file stands for C#.
- Class could have any number of methods, to further organize the code into smaller chunks.
- Classes are inside the namespace. The purpose of namespace is so we can potentially have multiple classes with the same name.
Rules about Method usage:
Method has a name (ex:
Main
) that unique and identic in each filesMethod can uses other methods, called calling a method
- When one method calls another method, it can pass some information (parameter) to work with inside the parathenses
- When the second method finished, it can pass information back to the first method
The type of information the method will pass back to its caller (ex:
void
)- void means that the method doesn't pass or return any information
- Other method types are String, int, Double, etc
Computer program is a collection of files, classes and methods.Each file can have multiple classes, and classes can have multiple methods.
How does computer know which code should be executed first?
In C#, it looks for method named Main.
There is feature in code editors that called syntax highlighting. Each text color represents something. Example, blue color in C# called keywords.
Keywords are special words that are reserved for language itself, means that we can't use them to named variable.
The more you hear and use these words, the more they'll stick with you, and the more fluent you'll become in C#