C# Fundamentals
Welcome to the C# fundamentals documentation. This section provides an introduction to the core concepts and building blocks of the C# programming language.
Introduction to C#
C# (pronounced "C sharp") is a modern, object-oriented programming language developed by Microsoft within the .NET initiative. It is a type-safe, object-oriented language that allows developers to build a wide range of applications, from desktop and web applications to mobile apps, cloud services, and games.
Key Concepts
- Variables and Data Types: Understanding how to declare variables and the different types of data C# supports (integers, strings, booleans, etc.).
- Operators: Learning about arithmetic, relational, logical, and assignment operators.
- Control Flow: Mastering conditional statements (
if
,else if
,else
,switch
) and loops (for
,while
,do-while
,foreach
). - Methods: Defining and calling methods to encapsulate reusable blocks of code.
- Classes and Objects: The foundation of object-oriented programming in C#.
Your First C# Program
Let's write a simple "Hello, World!" program:
using System;
public class HelloWorld
{
public static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}
Data Types in Detail
C# is statically typed, meaning variable types must be declared explicitly or inferred by the compiler. Here are some fundamental data types:
int
: For whole numbers (e.g., 10, -5).double
: For floating-point numbers with double precision (e.g., 3.14, -0.5).bool
: For true or false values.string
: For sequences of characters (e.g., "C# is awesome").char
: For single characters (e.g., 'A', '!').
Example: Variables and Output
This example demonstrates variable declaration and output:
using System;
public class VariablesExample
{
public static void Main(string[] args)
{
int age = 30;
string name = "Alice";
double salary = 50000.50;
Console.WriteLine($"Name: {name}");
Console.WriteLine($"Age: {age}");
Console.WriteLine($"Salary: {salary:C}"); // Formatted as currency
}
}
Next Steps
Now that you have a grasp of the basics, you can move on to more advanced topics like: