Table of Contents
1. Development Environment Setup
2. Starting with Hello World in C#
3. Understanding Classes and Namespaces
4. C# Language Fundamentals
5. Basic Method Concepts
- Development Environment Setup
Download Visual Studio
Download Visual Studio Tools - Free installation for Windows, Mac, Linux
https://visualstudio.microsoft.com/downloads/
The Community edition is free for personal use and sufficient for learning and personal projects.
- Starting with Hello World in C#
1. Console Application - Hello World
Create a new project
Select the appropriate development environment based on your needs
Write the code
using System;
namespace GreetingApp
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World");
Console.ReadKey();
}
}
}
using System;Imports the System namespace (simplifies code and improves development efficiency)
Without import:
System.Console.WriteLine()With import:
Console.WriteLine();
namespace GreetingApp {}Defines a namespace called GreetingApp
class Program {}Defines a class named Program
static void Main(string[] args) {}The main entry point of the program. In C#, there is typically one Main method, though multiple can exist.
Important: In C#, the Main method must have capital M. Main != main.
The Main method must be decorated with static, making it a static method that can execute without creating an instance.
Console.WriteLine();
Console.ReadKey();Outputs a line of text to the console
Pauses until a key is pressed
Press F5 to run in debug mode
Press Ctrl + F5 to run without debugging
2. Windows Forms Application - Hello World
Create a Windows Forms application
Wait for the project to load
Implementation code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsAppDemo
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void MainForm_Load(object sender, EventArgs e)
{
// Form loading logic here
}
private void btnDisplay_Click(object sender, EventArgs e)
{
lblMessage.Text = "Hello World";
}
}
}
Result:
How it works:
lblMessage.Text = "Hello World";Double-click the button in the events panel to implement the click event handler
lblMessage: The name assigned to the label control in the properties panel
Text: Used to get or set the text value (here we set it to "Hello World")
3. WPF Application - Hello World
Create a WPF application
Note: WPF is considered an upgraded version of Windows Forms
Remember to name your controls (good practice)
Add an event to the Button control
Click the lightning bolt icon to access events, double-click Click to create the event handler
Write code in the method to implement the click event
Implementation code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void btnGreet_Click(object sender, RoutedEventArgs e)
{
lblOutput.Text = "Hello World";
}
}
}
Result:
Click the button to display Hello World
- Understanding Classes and Namespaces
Class: The main building block of a program, representing a model of real-world entities
Namespace: Organizes classes (and other types) in a tree-like structure
Class vs Object relationship: An object (instance) is an instantiation of a class. A class is a concept, while an object is an individual instance of that concept. For example, "Animal" is a concept (class), while "Dog" and "Cat" are objects. You can say "Dog is an Animal" but you cannot say "Animal must be Dog or Cat".
1. Three Main Members of a Class
Property: Encapsulates the state of an object
Method: Encapsulates the behavior and functionality of an object
Event: Implements the observer pattern for loose coupling communication between objects
2. Using Classes and Namespaces
In a project, we create many classes and also use many predefined classes from the .NET framework. In Visual Studio, classes are highlighted in teal color.
Importing namespaces reduces code volume and improves development efficiency
Example:
With System namespace imported:
using System; namespace LibraryCatalog { class BookListing { static void Main(string[] args) { Console.WriteLine("What books are in the library?"); Console.WriteLine("Dream of the Red Chamber"); Console.WriteLine("Journey to the West"); Console.WriteLine("Water Margin"); Console.WriteLine("Romance of the Three Kingdoms"); } } }Imports System namespace
using System;Output statements
Console.WriteLine("What books are in the library?");Result:
Without System namespace imported:
//using System; namespace LibraryCatalog { class BookListing { static void Main(string[] args) { System.Console.WriteLine("What books are in the library?"); System.Console.WriteLine("Dream of the Red Chamber?"); System.Console.WriteLine("Journey to the West?"); System.Console.WriteLine("Water Margin?"); System.Console.WriteLine("Romance of the Three Kingdoms?"); } } }Comment out the import
//using System;Output statements
System.Console.WriteLine("What books are in the library?");If the System namespace is not imported, you must use the fully qualified namespace path every time you use types or methods from it.
When used incorrectly:
Place your cursor on the red squiggly line and press Alt + Enter, or click the light bulb with the red X
View the error message and select a solution
3. Class Conflicts in Namespaces
The purpose of importing namespaces is to simplify development and avoid class name conflicts. Different namespaces may contain classes with the same name, and not specifying them properly will cause conflicts.
If both System.Windows.Shapes and System.IO are imported
Do not import namespaces you don't use to avoid class conflicts.
4. Referencing Class Libraries
Class library references are the physical basis for using namespaces
References can be divided into two types: DLL references (black box, no source code) and Project references (white box, with source code)
Official Class Library Reference (Console Application)
1. Open the Object Browser
2. Select "All Components" and search for the required class library
3. Add to the current project, click the desired class library, and click Add
NuGet Package Reference
1. Right-click Dependencies and select Manage NuGet Packages
2. Search for the required package and download it. Check installed packages and updates in the top left
3. Benefits
Pre-integrated class libraries that don't require you to figure out relationships yourself. After installation, you can use them immediately.
DLL Reference (Black Box, No Source Code)
1. Right-click Dependencies and select Add Project Reference
2. Click Browse and find the required class library on your computer
3. View the class library: click View, then Object Browser. On the left side, see what namespaces and classes the library contains
4. Important Notes
Dependencies: DLL references require attention to whether the methods in the library are correct. If the referenced library contains erroneous methods, the entire project will fail to run.
Project Reference (White Box, With Source Code)
1. Open your created class library or find an open-source library in Visual Studio and import it into the project
2. Import the class library into the current project
4. C# Language Fundamentals
1. Keywords
2. Operators
3. Identifiers
4. Punctuators
5. Data Types
6. Comments
7. var for Type Inference
8. Variables (Member Variables)
9. Modifiers
10. Methods (Member Functions)
5. Basic Method Concepts
1. Origin of Methods
Creating a function in C:
int Add(int a, int b) { return a + b; }Creating a method in C#:
public int AddNumbers(int first, int second) { return first + second; }2. Method Reusability
Methods allow code to be written once and used multiple times
3. Method Overloading
Multiple methods can have the same name but different parameters
4. Constructors
Special methods called when an object is created
5. Operators
Symbols that perform operations on operands