Creating Your First Application
By the end of this lesson
Create, run and modify a console project, and understand what each file is for.
dotnet new console -o EmployeeTool
cd EmployeeTool
dotnet run- dotnet new console creates a console application. -o sets the output folder.
- dotnet run restores dependencies, compiles, and starts the program in one step.
Two files matter right now. Program.cs holds your code. EmployeeTool.csproj describes the project: which .NET version it targets and which packages it depends on.
Console.WriteLine("Employee Tool");
Console.WriteLine("=============");
string[] employees = { "Asha", "Ravi", "Meera" };
foreach (string employee in employees)
{
Console.WriteLine($"- {employee}");
}
Console.WriteLine($"{employees.Length} employees on file.");Run it again. You will carry this project forward: it becomes the employee management application that the course project builds on, and you will return to it repeatedly as you learn features that improve it.
Where is the Main method?
Older C# required every program to declare a class with a Main method as its entry point. Modern C# lets you write statements directly at the top of a file and generates that wrapper for you.
This matters when reading older code or tutorials, which will show the fuller form. It is not a different language — just less ceremony for small programs.
namespace EmployeeTool;
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Employee Tool");
}
}- Both forms compile to the same thing. The top-level version is preferred for small programs and entry points.
- Once an application grows past a single file, you will write classes explicitly anyway.
Summary
- dotnet new console scaffolds a project; dotnet run builds and starts it
- Program.cs holds code; the .csproj file describes the project and its dependencies
- Modern C# allows top-level statements; older code shows an explicit class with Main
- Parallel arrays that must stay aligned are a signal you need a class
Practice
Attempt each one before opening the solution. Getting it wrong first is how the idea sticks.
Try it yourself
Try it yourself
Add a second array holding each employee's role, and print each employee alongside their role.
You will need positions rather than foreach, since you are reading two arrays together.
Show solution
A for loop gives you the index needed to reach into both arrays. Note the fragility here: the two arrays must stay the same length and in the same order, which is exactly the problem classes solve in a later module.
string[] employees = { "Asha", "Ravi", "Meera" };
string[] roles = { "Engineer", "Designer", "Analyst" };
for (int i = 0; i < employees.Length; i++)
{
Console.WriteLine($"- {employees[i]} ({roles[i]})");
}Think about it
Think about it
Two parallel arrays that must stay aligned is a fragile design. What could go wrong, and what would a better structure look like?
Show solution
Anything that changes one array without the other breaks the pairing silently: sorting one, removing an item, adding to the wrong one. Nothing in the code prevents it and nothing reports it.
The better structure is a single collection of Employee objects, each holding a name and a role together, so they cannot become separated. That is precisely what the object-oriented module introduces, and this exercise is the problem it solves.
Saved in this browser only.