Every single feature of C# in 10 minutes
Table of Contents
Introduction
This tutorial provides a concise overview of C# programming language features, designed for beginners and those transitioning from other programming languages. By the end, you will have a solid understanding of C# syntax, data types, object-oriented programming concepts, and control flow statements.
Step 1: Understanding the Basics of C#
- C# is part of the C family of programming languages, utilizing curly brackets and semicolons.
- Developed by Microsoft, it is open-source and cross-platform, allowing applications to run on Windows, Linux, Mac, mobile (via Xamarin), and WebAssembly (via Blazor).
Step 2: Declaring Functions and Variables
- Functions in C# have a return type, a name, and can take arguments. The body is enclosed in curly brackets, and statements end with a semicolon.
void MyFunction(int parameter) { // function body }
- Use the
static
keyword for static functions. - Variables can be declared with specific types or using
var
for implicit typing, where the compiler determines the type:var myString = "Hello"; // Compiler infers string type var myNumber = 42; // Compiler infers int type
Step 3: Exploring Data Types
-
C# has built-in data types such as integers, floating-point numbers, and strings.
-
You can create custom data types using:
- Classes: Defined with the
class
keyword. - Structs: More efficient for primitive data types, stored on the stack.
- Records: Immutable data types that define a shape with fields.
Example of a record:
public record Person(string Name, int Age);
- Classes: Defined with the
Step 4: Using Access Modifiers
- Classes in C# can have five access modifiers:
public
: Accessible from anywhere.private
: Accessible only within the declaring class.protected
: Accessible within the declaring class and derived classes.internal
: Accessible within the same assembly.file
: Accessible only within the same file.
Step 5: Implementing Object-Oriented Principles
- C# supports inheritance, allowing classes to derive from others.
- Use the
abstract
keyword for classes that should only be inherited. - Interfaces define methods and properties that implementing classes must contain, declared with the
interface
keyword.
Step 6: Working with Properties and Constructors
- Properties can be declared using
get
andset
for encapsulation:public string Name { get; set; }
- Constructors initialize a new instance of a class:
public MyClass(string name) { Name = name; }
- Destructors clean up resources when an object is no longer needed.
Step 7: Control Flow Statements
- Use
if
,else if
, andelse
for conditional logic. - The
switch
statement can handle multiple conditions and pattern matching. - Looping structures include:
for
loopswhile
loopsforeach
loops for iterating over collections.
Step 8: Utilizing Collections
- C# offers arrays and various collections from the
System.Collections
namespace, such as:List<T>
: A dynamic array.Dictionary<TKey, TValue>
: A key-value pair collection.
Step 9: Async Programming and Generics
- Use
async
andawait
keywords for asynchronous code:public async Task MyAsyncMethod() { await Task.Delay(1000); }
- Generics allow for type-safe data structures and methods:
public class GenericClass<T> where T : class { // Class implementation }
Conclusion
You have now covered the essential features of C#, including its syntax, data types, object-oriented programming, control flow structures, and collections. To start coding in C#, install an IDE such as Visual Studio or Visual Studio Code, create a new console application using the command dotnet new console
, and begin experimenting with the concepts learned. For further learning, check out more resources and tutorials on C#.