C# Basics
C# Basics
This document serves as a comprehensive guide to the fundamentals of the C# programming language. It will
provide you with a thorough understanding of the core concepts, syntax, and best practices that are essential for
building robust and efficient applications using C#. The journey will start with a basic introduction to the language
and its core features, progressively delving into topics such as variables, data types, operators, control structures,
methods, arrays, and exception handling.
by jhon tonini
I troductio to C#
C# (pronounced "C sharp") is a modern, object-oriented programming language developed by Microsoft. It is a
powerful and versatile language, widely used for building a variety of applications, including desktop software,
web applications, mobile apps, games, and more. C# is designed to be easy to learn and use, yet powerful enough
to handle complex programming tasks. It is also a strongly-typed language, which means that the data type of a
variable must be explicitly defined.
One of the key advantages of C# is its close integration with the .NET Framework. The .NET Framework is a
comprehensive platform that provides a wide range of libraries and tools for developing various applications. This
integration makes C# a very powerful and efficient language for developing applications.
C# is known for its robust type system, automatic memory management (garbage collection), and support for
various programming paradigms, including object-oriented programming, generic programming, and functional
programming. These features make C# a versatile language suitable for building a wide range of applications.
Variable a d Data Type
Variables are used to store data in a C# program. Each variable has a name and a data type. The data type
specifies the kind of data that the variable can hold, such as integers, floating-point numbers, characters, or
strings.
For example, to declare an integer variable named `age` and assign the value 30 to it, you would use the following
code:
+ Addition 5+3=8
- Subtraction 10 - 5 = 5
* Multiplication 2 * 6 = 12
/ Division 10 / 2 = 5
% Modulo (remainder) 10 % 3 = 1
== Equal to 5 == 5 (true)
= Assignment x=5
Expressions can be used to perform calculations, compare values, and control the flow of execution in a C#
program.
Co trol Structure
Control structures are used to control the flow of execution in a C# program. They allow you to execute different
blocks of code based on certain conditions, or to repeat a block of code multiple times. Common control
structures in C# include:
These control structures allow you to create complex logic and algorithms in your C# programs.
1 2 3
Here's a simple example of a method in C# that calculates the sum of two numbers:
This method takes two integers as arguments and returns their sum. To call this method, you would use the
following code:
This code would call the `Add` method with the arguments `5` and `3`. The method would then return the value
`8`, which would be stored in the variable `result`. Methods help in writing reusable code, making programs more
modular and easier to understand.
Array a d Collectio
Arrays are used to store a collection of elements of the same data type. Collections, on the other hand, are a more
flexible and powerful way to store collections of objects. They provide a variety of methods for adding, removing,
and searching for elements. They are highly useful for managing dynamic data.
This code declares an array named `numbers` that can store 5 integer values. To access the elements of the array,
you can use their index, starting from 0. Collections come in different types, each with their specific advantages.
This code declares a List named `names` that can store strings. You can add elements to the list using the `Add`
method and access them using their index.
Arrays and collections are vital for storing and manipulating data efficiently in C# programs. They provide
structure and organization for your data, enabling you to work with large amounts of information effectively.
Exceptio Ha dli g
Exception handling is a crucial aspect of robust C# programming. It allows your program to handle unexpected
events or errors that might occur during execution. Exceptions can be caused by various factors, such as invalid
input, file errors, network problems, or even programming mistakes.
To handle exceptions, you use the `try`, `catch`, and `finally` blocks. The `try` block encloses the code that might
throw an exception. The `catch` block handles the exception if it occurs. The `finally` block executes regardless of
whether an exception was thrown or caught.
try
{
// Code that might throw an exception
int result = 10 / 0;
}
catch (DivideByZeroException ex)
{
// Handle the exception
Console.WriteLine("Error: Division by zero.");
}
finally
{
// Code to execute regardless of an exception
Console.WriteLine("This code always runs.");
}
In this example, the code in the `try` block throws a `DivideByZeroException` because it attempts to divide by
zero. The `catch` block catches the exception and prints an error message. The `finally` block always executes,
printing a message indicating that it always runs. Exception handling makes your C# programs more reliable by
preventing unexpected crashes and providing a mechanism to recover from errors.
Co clu io
This document has provided a fundamental understanding of C# programming, covering essential concepts like
variables, data types, operators, control structures, methods, arrays, collections, and exception handling. Now
that you have a solid foundation, you can explore more advanced topics like object-oriented programming,
generic programming, and more. The journey in mastering C# is an ongoing process of continuous learning and
practice. The C# programming language offers a vast ecosystem of libraries, frameworks, and tools, enabling you
to build a wide range of applications. Remember to practice regularly and explore different areas of C# to enhance
your programming skills.