C# Tutorial For Beginners - Learn C# Basics in 1 Hour
Table of Contents
Introduction
This tutorial is designed for beginners who want to learn the basics of C#. It covers fundamental concepts, the .NET framework, writing your first C# program, and understanding key programming principles like variables, constants, types, and operators. By the end of this tutorial, you will have a solid foundation in C# and be ready to explore more advanced topics.
Chapter 1: Difference between C# and .NET
-
C# as a Programming Language
- C# is a programming language used to write applications.
-
.NET Framework Overview
- The .NET framework is a platform for building applications. It includes:
- CLR (Common Language Runtime): Manages the execution of .NET programs.
- Class Library: Provides a set of reusable classes for various functionalities.
- The .NET framework is a platform for building applications. It includes:
-
Key Takeaway
- C# is one of multiple languages that can run on the .NET framework.
Chapter 2: Understanding CLR
-
Native Code vs. Intermediate Language
- Traditional languages like C++ compile code to native machine code, limiting portability.
- C# compiles to an Intermediate Language (IL), which is platform-independent.
-
Role of CLR
- The CLR converts IL to native code through Just-In-Time (JIT) compilation, allowing C# applications to run on any machine with CLR installed.
Chapter 3: Architecture of .NET Applications
- Classes, Namespaces, and Assemblies
- Classes: Define data and methods; think of them as blueprints (e.g., a Car class with attributes like make and model).
- Namespaces: Organize related classes (e.g., System.Collections for collections).
- Assemblies: The physical output of a compiled application, either as an executable or DLL.
Chapter 4: Your First C# Program
-
Setting Up Visual Studio
- Open Visual Studio and create a new project.
- Select "Console Application" from the templates.
- Name your project (e.g., "HelloWorld") and specify a location.
-
Understanding the Project Structure
- The main code file is
Program.cswhich contains:- A namespace declaration.
- A class named
Program. - A static
Mainmethod, the entry point of the program.
- The main code file is
-
Writing Your First Program
using System; namespace HelloWorld { class Program { static void Main(string[] args) { Console.WriteLine("Hello, World!"); } } }- Run the program (Ctrl + F5) to see "Hello, World!" printed in the console.
Chapter 5: Variables and Constants
-
Variables
- A variable is a named storage location. Declare with type followed by an identifier.
int number; // Declaration number = 5; // Initialization -
Constants
- A constant is an immutable value set at compile time.
const float Pi = 3.14f; // Declaration -
Best Practices
- Use meaningful names (e.g.,
firstNameinstead offn). - Follow naming conventions (CamelCase for variables, PascalCase for constants).
- Use meaningful names (e.g.,
Chapter 6: Type Conversion
-
Implicit and Explicit Conversion
- Implicit: Automatic conversion without data loss.
int num = 10; float fNum = num; // Implicit conversion - Explicit (Casting): Required when data loss may occur.
double dNum = 9.78; int intNum = (int)dNum; // Explicit conversion
- Implicit: Automatic conversion without data loss.
-
Non-Compatible Types
- Use
Convertclass orParsemethod for incompatible types.
string strNumber = "123"; int num = Convert.ToInt32(strNumber); // Using Convert - Use
Chapter 7: Operators
-
Types of Operators in C#
- Arithmetic Operators:
+,-,*,/,% - Comparison Operators:
==,!=,>,<,>=,<= - Logical Operators:
&&,||,! - Bitwise Operators:
&,|,^
- Arithmetic Operators:
-
Examples
int a = 10, b = 5; int sum = a + b; // Addition bool isEqual = (a == b); // Comparison
Conclusion
You have now learned the foundational concepts of C#, including the differences between C# and .NET, the role of CLR, how to create your first program, and the basics of variables, constants, and operators. The next step is to practice coding in C# using the concepts learned here and gradually explore more advanced topics such as object-oriented programming, data structures, and algorithms. Happy coding!