C# Language Documentation
This section provides comprehensive documentation for the C# programming language, covering its fundamental concepts, advanced features, and best practices. C# is a modern, object-oriented, and type-safe programming language developed by Microsoft that enables developers to build a wide range of applications for the .NET platform.
Introduction to C#
C# (pronounced "C-sharp") is a versatile language designed for creating applications that run on the .NET Framework and .NET Core. It offers a balance of high-level productivity and low-level control, making it suitable for everything from web and mobile applications to desktop software and game development.
Key characteristics of C# include:
- Object-Oriented: Based on classes, objects, inheritance, polymorphism, and encapsulation.
- Type-Safe: Enforces strong typing to prevent common programming errors.
- Component-Oriented: Designed for building reusable software components.
- Managed: Runs under the control of the Common Language Runtime (CLR), which provides memory management and other services.
- Modern: Continuously evolving with new features and improvements in each version.
Language Basics
Understanding the foundational elements of C# is crucial for effective programming.
Variables and Data Types
Variables are containers for storing data values. C# is a statically-typed language, meaning each variable must have a declared data type before it can be used. This helps the compiler catch errors early.
Common Value Types:
int
: Whole numbers (e.g.,10
,-5
).double
: Floating-point numbers (e.g.,3.14
,-0.5
).bool
: Boolean values (true
orfalse
).char
: Single characters (e.g.,'A'
,'%'
).struct
: User-defined value types.
Common Reference Types:
string
: Sequences of characters (e.g.,"Hello, World!"
).class
: User-defined reference types.array
: Fixed-size sequences of elements of the same type.object
: The base type for all types in C#.
Example: Declaring Variables
// Declaring an integer variable
int age = 30;
// Declaring a string variable
string name = "Alice";
// Declaring a boolean variable
bool isActive = true;
// Using variables
Console.WriteLine($"Name: {name}, Age: {age}, Active: {isActive}");
Operators
Operators are symbols that perform operations on variables and values. C# supports various types of operators:
- Arithmetic:
+
,-
,*
,/
,%
- Comparison:
==
,!=
,<
,>
,<=
,>=
- Logical:
&&
(AND),||
(OR),!
(NOT) - Assignment:
=
,+=
,-=
, etc. - Increment/Decrement:
++
,--
Control Flow
Control flow statements determine the order in which code is executed.
- Conditional Statements:
if
,else if
,else
,switch
- Loops:
for
,foreach
,while
,do-while
- Branching Statements:
break
,continue
,return
Example: If-Else Statement
int score = 75;
if (score >= 90) {
Console.WriteLine("Grade: A");
} else if (score >= 80) {
Console.WriteLine("Grade: B");
} else {
Console.WriteLine("Grade: C or lower");
}
Object-Oriented Programming (OOP)
C# is a powerful object-oriented language. OOP principles help in organizing complex code into reusable and maintainable components.
Classes and Objects
A class is a blueprint for creating objects. An object is an instance of a class. Classes define properties (data members) and methods (behavior).
Example: Class and Object
public class Car {
public string Make;
public string Model;
public int Year;
public void StartEngine() {
Console.WriteLine($"The {Year} {Make} {Model}'s engine has started.");
}
}
// Creating an object of the Car class
Car myCar = new Car();
myCar.Make = "Toyota";
myCar.Model = "Camry";
myCar.Year = 2022;
myCar.StartEngine();
Inheritance
Inheritance allows a class (derived class or child class) to inherit properties and methods from another class (base class or parent class). It promotes code reuse.
Polymorphism
Polymorphism (meaning "many forms") allows objects of different classes to be treated as objects of a common base class. This is often achieved through method overriding and interfaces.
Encapsulation
Encapsulation is the bundling of data (fields) and methods that operate on the data within a single unit (class). It restricts direct access to some of the object's components, protecting data integrity.
Abstraction
Abstraction is the process of hiding complex implementation details and showing only the essential features of an object. It is achieved through abstract classes and interfaces.
Advanced Features
C# offers numerous advanced features that enable developers to write more efficient, robust, and expressive code.
Generics
Generics allow you to define classes, interfaces, methods, and delegates that operate on types specified as parameters. This enables code reuse and type safety.
Language Integrated Query (LINQ)
LINQ provides a powerful and consistent way to query data from various sources (collections, databases, XML) directly within the C# language.
Example: LINQ Query
List<int> numbers = new List<int> { 5, 1, 8, 3, 6, 2 };
// Select numbers greater than 4
var query = from num in numbers
where num > 4
orderby num descending
select num;
foreach (var n in query) {
Console.WriteLine(n); // Output: 8, 6, 5
}
Asynchronous Programming
The async
and await
keywords simplify writing asynchronous code, enabling applications to remain responsive during long-running operations.
Delegates and Events
Delegates are type-safe function pointers, used to pass methods as arguments. Events are a mechanism that allows a class to notify other classes when something happens.
Attributes
Attributes provide a way to add declarative information to code elements, such as classes, methods, and properties. This information can be accessed at runtime using reflection.
Language Reference
For detailed specifications on C# syntax, keywords, and libraries, please refer to the official C# Language Specification.
Tutorials
Explore practical tutorials to learn C# by building various types of applications.