Basic Functions of C#
Introduction
C# (pronounced C-Sharp) is a modern, object-oriented programming language developed
by Microsoft. It is widely used for developing desktop applications, web applications, and
games. Below are some basic functions of C# along with examples.
1. Hello World
The 'Hello World' program is the simplest program in C#. It demonstrates the basic
structure of a C# application.
Example:
using System;
class Program
{
static void Main()
{
Console.WriteLine("Hello, World!");
}
}
2. Variables and Data Types
Variables store data in memory, and each variable has a type that determines the kind of
data it can hold.
Example:
int age = 25;
double height = 5.9;
string name = "John";
bool isActive = true;
Console.WriteLine($"Name: {name}, Age: {age}, Height: {height}, Active: {isActive}");
3. Conditional Statements
Conditional statements allow you to execute certain blocks of code based on conditions.
Example:
int number = 10;
if (number > 0)
{
Console.WriteLine("The number is positive.");
}
else
{
Console.WriteLine("The number is negative.");
}
4. Loops
Loops allow you to execute a block of code multiple times.
Example (for loop):
for (int i = 1; i <= 5; i++)
{
Console.WriteLine($"Iteration: {i}");
}
5. Functions
Functions (or methods) are blocks of code that perform a specific task.
Example:
int AddNumbers(int a, int b)
{
return a + b;
}
Console.WriteLine(AddNumbers(3, 5));