<\> C# For Beginners
<\> C# For Beginners
C# යනු ?
C# යනු Microsoft සමාගම විසින් නිර්මාණය කළ object-oriented (ඔබ්ජෙක්ට්-ඕරියෙන්ටඩ්) ප්රායෝගික ප්රෝග්රෑමින් භාෂාවකි. .NET Framework එකේදී වැඩිම ලෙස භාවිතා වේ. C# භාවිතා කර, Windows, Web, සහ Mobile යෙදුම් සාදිය හැක.
• Visual Studio ස්ථාපනය කිරීම ,
Visual Studio යනු Microsoft විසින් නිර්මාණය කළ Integrated Development Environment (IDE) එකකි. මේ IDE එක C# භාෂාවෙන් වැඩිම හා සාර්ථකම භාවිතා කරේය. Visual Studio Download කර ස්ථාපනය කිරීම සඳහා https://visualstudio.microsoft.com භාවිතා කරන්න.
පළමු වැඩසටහන නිර්මාණය ,
"Hello World" වැඩසටහන.
මෙය Visual Studio හි Console Application එකක් ලෙස සාදමු
using System;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
```
• කේතය විස්තර කිරීම,
- using System; : System නමැති Namespace එක භාවිතා කිරීම.
- namespace HelloWorld : "HelloWorld" නමැති Namespace එකක් සාදයි.
- class Program : "Program" නමැති Class එකක් සාදයි.
- static void Main(string[] args) : මෙය Main Method එකයි. වැඩසටහන ක්රියාත්මක වන විට මුලින්ම මෙය ක්රියාත්මක වේ.
- Console.WriteLine("Hello World!"); : "Hello World!" පණිවිඩය Console එකේ දැක්වීමට භාවිතා කරයි.
• Variables and Data Types ,
මෙම Variables සහ data types භාවිතා කර විවිධ ආකාරයේ දත්ත ගබඩා කළ හැක.
උදාහරණ:
int age = 25; // Integer
double salary = 45500.50; // Double
char grade = 'A'; // Character
string name = "John"; // String
bool isMarried = false; // Boolean
```
• මූලික දත්ත වර්ග,
- int : පූර්ණ සංඛ්යා.
- double : දශම සංඛ්යා.
- char : එක් අක්ෂරයක්.
- string : අක්ෂර පෙලක්.
- bool : සත්ය හෝ අසත්ය බව.
• ක්රියාකාරකම් (Operators),
1- ගණිත කාර්යයන් (Arithmetic Operators)
int x = 10;
int y = 5;
Console.WriteLine(x + y); // එකතු කිරීම
Console.WriteLine(x - y); // ඍණ කිරීම
Console.WriteLine(x * y); // ගුණ කිරීම
Console.WriteLine(x / y); // බෙදීම
Console.WriteLine(x % y); // ප්රතිශත
```
2 - සැසදීමේ කාර්යයන් (Comparison Operators)
Console.WriteLine(x == y); // සමානදැයි පරීක්ෂා කිරීම
Console.WriteLine(x != y); // සමාන නොවීම පරීක්ෂා කිරීම
Console.WriteLine(x > y); // වඩාත් වැඩිදැයි පරීක්ෂා කිරීම
Console.WriteLine(x < y); // වඩාත් අඩුදැයි පරීක්ෂා කිරීම
Console.WriteLine(x >= y); // වඩාත් වැඩි හෝ සමානදැයි පරීක්ෂා කිරීම
Console.WriteLine(x <= y); // වඩාත් අඩු හෝ සමානදැයි පරීක්ෂා කිරීම
```
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- මූලික methods
class Program
{
static void Greet()
{
Console.WriteLine("Hello!");
}
static void Main(string[] args)
{
Greet(); // ක්රමය කැඳවීම
}
}
```
2- පරාමිතීන් සහිත Methods
class Program
{
static void Greet(string name)
{
Console.WriteLine("Hello, " + name + "!");
}
static void Main(string[] args)
{
Greet("John"); // ක්රමය කැඳවීම
}
}
```
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); // ප්රතිඵලය පෙන්වීම
}
}
```
• Classes and Objects ,
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.");
}
}
```
Object එකක් සෑදීම ,
class Program
{
static void Main(string[] args)
{
Person person = new Person();
person.Name = "John";
person.Age = 30;
person.Introduce();
}
}
```
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 !