0% found this document useful (0 votes)
25 views

C# Basics

c# basics

Uploaded by

jhon tonini
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

C# Basics

c# basics

Uploaded by

jhon tonini
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

C# Ba ic

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.

Integer (int): Stores whole numbers, such as 10, 25, or -5.


Floating-point (double): Stores numbers with decimal places, such as 3.14, 2.718, or -1.5.
Character (char): Stores a single character, such as 'A', 'b', or '!'
String (string): Stores a sequence of characters, such as "Hello, world!".

To declare a variable, you use the following syntax:

data_type variable_name = value;

For example, to declare an integer variable named `age` and assign the value 30 to it, you would use the following
code:

int age = 30;


Operator a d Expre io
Operators are symbols that perform specific operations on variables and values. They are used to create
expressions, which evaluate to a single value. C# supports a wide range of operators, including arithmetic
operators, comparison operators, logical operators, and assignment operators.

Operator Description Example

+ 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)

!= Not equal to 5 != 6 (true)

> Greater than 10 > 5 (true)

< Less than 5 < 10 (true)

>= Greater than or equal to 5 >= 5 (true)

<= Less than or equal to 5 <= 10 (true)

&& Logical AND true && true (true)

|| Logical OR true || false (true)

! Logical NOT !true (false)

= 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:

If statement: Executes a block of code if a specified condition is true.


Else statement: Executes a block of code if the condition in the corresponding if statement is false.
Else if statement: Provides additional conditions to check if the previous if or else if statements were false.
Switch statement: Executes a block of code based on the value of a variable.
For loop: Repeats a block of code a specified number of times.
While loop: Repeats a block of code as long as a specified condition is true.
Do-while loop: Executes a block of code at least once, and then repeatedly executes it as long as a specified
condition is true.

These control structures allow you to create complex logic and algorithms in your C# programs.

1 2 3

If State e t For Loop W ile Loop


Checks a condition and Repeats a block of code a Repeats a block of code as long
executes a block of code if the specified number of times. as a specified condition is true.
condition is true.
Met od a d Fu ctio
Methods, also known as functions, are blocks of reusable code that perform specific tasks. They are defined using
the `static` keyword and a return type. They are essential for breaking down complex programs into smaller, more
manageable units. A well-structured program uses methods effectively to improve code organization and
readability. A method can take inputs called arguments (or parameters) and can return a value. This is done using
the return keyword.

Here's a simple example of a method in C# that calculates the sum of two numbers:

static int Add(int num1, int num2)


{
return num1 + num2;
}

This method takes two integers as arguments and returns their sum. To call this method, you would use the
following code:

int result = Add(5, 3);

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.

Here's an example of declaring an array of integers in C#:

int[] numbers = new int[5];

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.

Here's an example of declaring a List in C#:

List<string> names = new List<string>();

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.

Here's an example of exception handling in C#:

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.

Practice Regularly Explore Re ource E gage wit t e Build Project


Consistent practice is key Take advantage of Co u ity Apply your knowledge by
to mastering C# and available resources such Connect with other C# building real-world
becoming a proficient as online tutorials, developers, ask projects to gain practical
programmer. documentation, and questions, and share your experience and solidify
books to deepen your knowledge. The your understanding.
understanding of C#. community is a valuable
resource for learning and
growth.

You might also like