Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
9 views

Expanded_Basic_Functions_of_CSharp

This is the basic function of #sharp

Uploaded by

It's Judy
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Expanded_Basic_Functions_of_CSharp

This is the basic function of #sharp

Uploaded by

It's Judy
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

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

6. Arrays
Arrays store multiple values of the same type in a single variable.

Example:
int[] numbers = {1, 2, 3, 4, 5};

foreach (int num in numbers)


{
Console.WriteLine(num);
}

7. Classes and Objects


Classes are templates for creating objects, and objects are instances of classes.

Example:

class Person
{
public string Name;
public int Age;

public void DisplayInfo()


{
Console.WriteLine($"Name: {Name}, Age: {Age}");
}
}

Person person = new Person();


person.Name = "Alice";
person.Age = 30;
person.DisplayInfo();

8. Inheritance
Inheritance allows a class to inherit methods and properties from another class.

Example:

class Animal
{
public void Eat()
{
Console.WriteLine("This animal eats food.");
}
}
class Dog : Animal
{
public void Bark()
{
Console.WriteLine("The dog barks.");
}
}

Dog myDog = new Dog();


myDog.Eat();
myDog.Bark();

9. Exception Handling
Exception handling helps manage runtime errors in a program.

Example:

try
{
int number = int.Parse("NotANumber");
}
catch (FormatException)
{
Console.WriteLine("Input string is not a valid number.");
}

10. File Handling


File handling allows you to read from or write to files.

Example:

using System.IO;

File.WriteAllText("example.txt", "Hello, File Handling!");


string content = File.ReadAllText("example.txt");
Console.WriteLine(content);

Comprehensive Example
This example combines multiple basic functions in a single program:
using System;
using System.IO;

class Animal
{
public string Name;
public void Eat()
{
Console.WriteLine($"{Name} is eating.");
}
}

class Dog : Animal


{
public void Bark()
{
Console.WriteLine($"{Name} barks.");
}
}

class Program
{
static void Main()
{
Dog myDog = new Dog();
myDog.Name = "Buddy";
myDog.Eat();
myDog.Bark();

int[] numbers = {1, 2, 3, 4, 5};


foreach (int num in numbers)
{
Console.WriteLine($"Number: {num}");
}

try
{
int result = 10 / 0;
}
catch (DivideByZeroException)
{
Console.WriteLine("Cannot divide by zero!");
}

File.WriteAllText("output.txt", "This is a comprehensive example.");


Console.WriteLine(File.ReadAllText("output.txt"));
}
}

You might also like