<\> C# For Beginners
<\> C# For Beginners
What is C#?
C# is an object-oriented programming language developed by Microsoft. It is primarily used within the .NET Framework. With C#, you can create applications for Windows, Web, and Mobile platforms.
• Installing Visual Studio ,
Visual Studio is an Integrated Development Environment (IDE) developed by Microsoft. This IDE is most commonly and successfully used with the C# language. To download and install Visual Studio, visit https://visualstudio.microsoft.com.
Creating the First Program ,
"Hello World" Program
Let's create this as a Console Application in Visual Studio.
using System;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
```
Code Explanation:
using System;: This imports the System namespace.
namespace HelloWorld: Creates a namespace named "HelloWorld".
class Program: Creates a class named "Program".
static void Main(string[] args): This is the Main method. It runs first when the program executes.
Console.WriteLine("Hello World!");: Displays the "Hello World!" message on the console.
You can use variables and data types to store different types of data in C#. Example:
Examples :
int age = 25; // Integer
double salary = 45500.50; // Double
char grade = 'A'; // Character
string name = "John"; // String
bool isMarried = false; // Boolean
```
Basic Data Types:
int: Integer values.
double: Decimal values.
char: A single character.
string: A sequence of characters (text).
bool: Represents true or false.
Arithmetic Operators:
1- ගණිත කාර්යයන් (Arithmetic Operators)
int x = 10;
int y = 5;
Console.WriteLine(x + y); // Addition
Console.WriteLine(x - y); // Subtraction
Console.WriteLine(x * y); // Multiplication
Console.WriteLine(x / y); // Division
Console.WriteLine(x % y); // Modulus (remainder)
```
2 - Comparison Operators
Console.WriteLine(x == y); // Check if equal
Console.WriteLine(x != y); // Check if not equal
Console.WriteLine(x > y); // Check if greater
Console.WriteLine(x < y); // Check if smaller
Console.WriteLine(x >= y); // Check if greater or equal
Console.WriteLine(x <= y); // Check if smaller or equal
```
3 - Logical Operators
bool a = true;
bool b = false;
Console.WriteLine(a && b); // AND
Console.WriteLine(a || b); // OR
Console.WriteLine(!a); // NOT
```
4- Conditional Statements
if ,
int number = 10;
if (number > 0)
{
Console.WriteLine("Positive Number");
}
```
if-else
if (number > 0)
{
Console.WriteLine("Positive Number");
}
else
{
Console.WriteLine("Non-Positive Number");
}
```
if-else if-else ,
if (number > 0)
{
Console.WriteLine("Positive Number");
}
else if (number < 0)
{
Console.WriteLine("Negative Number");
}
else
{
Console.WriteLine("Zero");
}
```
• Loop Statements ,
for ,
for (int i = 1; i <= 5; i++)
{
Console.WriteLine(i);
}
```
while
int j = 1;
while (j <= 5)
{
Console.WriteLine(j);
j++;
}
```
do-while
int k = 1;
do
{
Console.WriteLine(k);
k++;
} while (k <= 5);
```
• Methods ,
1- Basic methods
class Program
{
static void Greet()
{
Console.WriteLine("Hello!");
}
static void Main(string[] args)
{
Greet(); // Calling the method
}
}
```
2- Methods
class Program
{
static void Greet(string name)
{
Console.WriteLine("Hello, " + name + "!");
}
static void Main(string[] args)
{
Greet("John"); // Calling the method
}
}
```
3 - Returning Methods
class Program
{
static int Add(int a, int b)
{
return a + b;
}
static void Main(string[] args)
{
int result = Add(5, 3);
Console.WriteLine(result); // Displaying the result
}
}
```
• Classes and Objects ,
Creating a Class:
class Person
{
public string Name;
public int Age;
public void Introduce()
{
Console.WriteLine("Hello, my name is " + Name + " and I am " + Age + " years old.");
}
}
```
Creating an Object:
class Program
{
static void Main(string[] args)
{
Person person = new Person();
person.Name = "John";
person.Age = 30;
person.Introduce();
}
}
```
Using a Constructor:
class Person
{
public string Name;
public int Age;
public Person(string name, int age)
{
Name = name;
Age = age;
}
public void Introduce()
{
Console.WriteLine("Hello, my name is " + Name + " and I am " + Age + " years old.");
}
}
class Program
{
static void Main(string[] args)
{
Person person = new Person("John", 30);
person.Introduce();
}
}
```
• Inheritance
class Animal
{
public void Eat()
{
Console.WriteLine("Eating...");
}
}
class Dog : Animal
{
public void Bark()
{
Console.WriteLine("Barking...");
}
}
class Program
{
static void Main(string[] args)
{
Dog dog = new Dog();
dog.Eat();
dog.Bark();
}
}
```
• Orphism & Interface
Interface ,
interface IAnimal
{
void MakeSound();
}
class Dog : IAnimal
{
public void MakeSound()
{
Console.WriteLine("Barking...");
}
}
class Cat : IAnimal
{
public void MakeSound()
{
Console.WriteLine("Meowing...");
}
}
class Program
{
static void Main(string[] args)
{
IAnimal dog = new Dog();
dog.MakeSound();
IAnimal cat = new Cat();
cat.MakeSound();
}
}
… … … … …
New Lessons Coming Soon !