C# Language Overview
C# is a modern, object-oriented, and type-safe programming language developed by Microsoft. It runs on the .NET platform and is designed for building a wide range of applications, from desktop and web applications to mobile apps and cloud services. C# combines the power of C++ with the ease of use of Visual Basic.
Key Features of C#
- Object-Oriented: C# supports encapsulation, inheritance, and polymorphism, allowing for modular and reusable code.
- Type-Safe: The compiler enforces type checking at compile time, reducing runtime errors.
- Component-Oriented: C# encourages the development of reusable software components.
- Modern Language Constructs: Features like LINQ, async/await, and pattern matching simplify complex programming tasks.
- Garbage Collection: Automatic memory management frees developers from manual memory allocation and deallocation.
- Interoperability: C# integrates seamlessly with other languages and COM components.
Basic Syntax and Structure
A typical C# program consists of classes, which contain methods. Methods define the executable code. Here's a very simple "Hello, World!" example:
using System;
public class HelloWorld
{
public static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}
In this example:
using System;imports theSystemnamespace, which contains essential classes likeConsole.public class HelloWorlddeclares a class namedHelloWorld.public static void Main(string[] args)is the entry point of the application. TheMainmethod is executed when the program starts.Console.WriteLine("Hello, World!");prints the string "Hello, World!" to the console.
Fundamental Data Types
C# provides a rich set of built-in data types:
- Integral Types:
sbyte,byte,short,ushort,int,uint,long,ulong - Floating-Point Types:
float,double,decimal - Boolean Type:
bool(trueorfalse) - Character Type:
char(a single Unicode character) - String Type:
string(a sequence of Unicode characters)
Example declaration:
int age = 30;
string name = "Alice";
double price = 19.99;
bool isActive = true;
Variables and Constants
Variables are used to store data. Constants, declared using the const keyword, store values that cannot be changed after they are initialized.
// Variable
int counter = 10;
counter = 20; // Valid
// Constant
const double PI = 3.14159;
// PI = 3.0; // This would cause a compile-time error
Control Flow Statements
C# offers various control flow statements to manage the execution path of your program:
- Conditional Statements:
if,else if,else,switch - Looping Statements:
for,foreach,while,do-while - Branching Statements:
break,continue,return,goto
Object-Oriented Programming (OOP) Concepts
C# fully embraces OOP principles:
- Classes and Objects: Classes are blueprints, and objects are instances of those blueprints.
- Encapsulation: Bundling data (fields) and methods that operate on the data within a single unit (class).
- Inheritance: Allowing a new class to inherit properties and behaviors from an existing class.
- Polymorphism: The ability of an object to take on many forms, often through method overriding and overloading.
Further Reading
This overview provides a glimpse into the C# language. For a deeper understanding, explore the following topics: