Introduction to C#
Instructor: Venkat
Overview
- This is Part 1 of the C# introduction module.
- Focus will be on the basic structure of a C# program and the concept of namespaces, particularly the main method.
Setting Up Visual Studio
- Launch Visual Studio 2010.
- Create a new project:
- Go to File > New Project.
- Choose C# as the programming language.
- Select Console Application type.
- Name the project "Introduction to C#" and save it in the C Drive.
Basic C# Program Structure
- Aim: Print a message to the console.
- The console refers to the command prompt window, accessed by typing
CMD
in the run dialog.
Writing the Program
- The program should consist of minimal code to achieve the desired output.
- Remove unnecessary namespace declarations and parameters in the main method.
- Use the
Console
class from the .NET Framework to display messages.
- Key method:
WriteLine()
to print messages.
- Example:
Console.WriteLine("Welcome to C# training");
- To run the program, press Ctrl + F5.
Understanding Namespaces
- Namespace Declaration:
- Introduces a namespace to use classes within it.
- Example:
using System;
- A namespace is a collection of classes, interfaces, structures, enums, and delegates.
- The purpose of namespaces: Organizing code.
- Without the namespace declaration, you must use the fully qualified name for classes.
- Example: Fully qualified name for the
Console
class is System.Console
.
Important Concepts
Class
- All C# code must reside within a class.
- Discussed types of classes: static and instance classes.
Main Method
- The main method serves as the entry point for program execution.
- Only one main method can exist per application.
- Example of main method syntax:
static void Main(string[] args)
- If there are multiple methods named "main", only the correctly defined main method will run (the one without a suffix).
Execution Flow
- Execution starts at the main method and ends when the program reaches the end of the method.
- To call another method within the main method, it can be done directly, ensuring both messages are printed.
Summary
- Key components of a simple C# program:
- Namespace declaration.
- Class definition.
- Main method as the entry point of the application.
- Importance of using namespaces for code organization.
Additional Resources
- Check the website for more resources.
- For interview questions, visit the provided link.