C# Notes
C# Notes
Class is like a container that have store a data that and contain and related method.
namespace Helloworld
{
public class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World");
// other class
using System;
namespace Operation
{
Now in this code you have to see that a static method have to access in to main file.
namespace Helloworld
{
public class Program
{
static void Main (string [] args)
{
Console.WriteLine("Hello World");
}
}
}
using System;
namespace Operation
{
public class Calculator
{
public int num1;
int num2=30;
int result;
public void Add()
{
result = num1 + num2;
Console.WriteLine(result);
Console.ReadLine();
}
public void Subtract()
{
int result = num1 - num2;
Console.WriteLine(result);
Console.ReadLine();
}
public void Multiply()
{
int result = num1 * num2;
Console.WriteLine(result);
Console.ReadLine();
}
}
}
How can return function will be define and and how can access?
using System;
namespace Operation
{
public class Calculator
{
}
//public void Subtract()
//{
// int result = num1 - num2;
// Display();
//}
//public void Multiply()
//{
// int result = num1 * num2;
// Display();
//}
public void Display(int result)
{
Console.WriteLine("Result of number "+ result);
Console.ReadLine();
}
}
}
using Operation;
using System;
namespace Helloworld
{
public class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World");
Calculator calculator = new Calculator();
int a = 10;
int b = 10;
int g = calculator. Add(a, b); // return method access they have to pass the arguments and also store the another
variable
Console.WriteLine(g);
}
}
}
namespace Helloworld
{
public class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World");
int result = First500prime();
Console.WriteLine("Sum of the first 500 prime numbers: " + result);
Inheritance
Inheritance is a key concept in object-oriented programming (OOP) that allows a class (known as a
subclass or derived class) to inherit properties and behaviors (methods) from another class (known as a
superclass or base class).
Single inheritance:
in which one parent calss and one child calss
Multilevel inheritance:
namespace helloworld
{
class progrmmer
{
class mobile
{
public virtual void specifications()
{
console.writeline("mobile specifications");
}
}
switch (input)
{
case "vivo":
mobile = new vivo();
break;
case "samsung":
mobile = new samsung();
break;
case "techno":
mobile = new techno();
break;
default:
console.writeline("invalid input:");
return;
}
mobile.specifications();
}
}
}
MUltilaevel inheritance:
public class A
{
public void MethodA()
{
Console.WriteLine("MethodA in Class A");
}
}
public class B : A
{
public void MethodB()
{
Console.WriteLine("MethodB in Class B");
}
}
public class C : B
{
public void MethodC()
{
Console.WriteLine("MethodC in Class C");
}
}
class Program
{
static void Main(string[] args)
{
C objC = new C();
objC.MethodA(); // Accessing MethodA from class A
objC.MethodB(); // Accessing MethodB from class B
objC.MethodC(); // Accessing MethodC from class C
}
}
Output:
MethodA in Class A
MethodB in Class B
MethodC in Class C
Muilplie inheritance cannot support directly beasuse ambiguty kki waja sa we can achieve multiple
inheritance througth interfacese.
Interfaces:
In interface you have define those method other class has nesserly have use this method.
With the help of we ca achieve abstaration and muiltple inheritance can also achieve.
Object always for a classs not interface can define a object(interface ka hum object ni bana sakta hain).
Interface is like a contract ha jis ma hum contract define karata jis classes ka sath or class ko wo contract
fulfil krna necessary hota ha.
Encapsulation:
Encapsulation is like a hiding the value. Encapsulation have achieve from two methods that
have getter and setter method.
If you have no use of setter and geeter method that you have use C# properties like set and get
method.
C# propertises that have only return the values.
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World");
//int result = First500prime();
//Console.WriteLine("Sum of the first 500 prime numbers: " + result);
}
class Encapsulation
{
accountBalance = value;
}
public void getter()
{
Console.WriteLine(accountBalance);
}
namespace Helloworld
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World");
//int result = First500prime();
//Console.WriteLine("Sum of the first 500 prime numbers: " + result);
}
class Encapsulation
{
}
}
Polymorphism:
Car car = new Car();
Cycle cycle = new Cycle();
Booat booat = new Booat();
Car[] vechile = { car, cycle, booat }; //Car, Cycle, and Boat are all derived from Vehicle, but they are not derived from
each other.
Other Example
var carobje = new Car();
// carobje = GetCar("Honda");
// carobje = GetCar("Mehran");
class mobile
{
public virtual void specifications()
{
console.writeline("mobile specifications");
}
}
switch (input)
{
case "vivo":
mobile = new vivo();
break;
case "samsung":
mobile = new samsung();
break;
case "techno":
mobile = new techno();
break;
default:
console.writeline("invalid input:");
return;
}
mobile.specifications();
}
}
}
Arrays Program
#include<iostream>
for(int i=0;i<size;i++)
for(int j=i+1;j<size;j++)
if(array[i]>array[j])
int temp=array[i];
array[i]=array[j];
array[j]=temp;
Sortarray(array,size);
int count;
int temparray[10];
int tempsize=0;
for(int i=0;i<size;i=i+count)
count=1;
for(int j=i+1;j<size;j++)
if(array[i]==array[j])
count++;
temparray[tempsize]=count;
tempsize++;
if (temparray[i] == temparray[j]) {
// Found a duplicate
return false;
// No duplicates found
return true;
int main()
int array[] = {3, 2, 3, 2, 1, 6, 6, 1, 5}; // Ensure every number except one appears twice
Linklist Program
Linklist is linear data structure that have create nodes each node has store data and its address.
// in this code element has add in first insertion measn add element add first in node
#include<iostream>
using namespace std;
class Node
{
public:
int value;
Node *next;
Node(int data)
{
value=data;
next=NULL;
}
};
int main()
{
Node *Head=nullptr;
int array[]={1,2,3,4,5};
for(int i=0;i<5;i++)
{
if(Head==NULL)
{
Head = new Node(array[i]);
}
else
{
Node *temp;
temp=new Node(array[i]);
temp->next=Head;
Head=temp;
}
}
class Node
{
public:
int value;
Node *next;
Node(int value)
{
this->value = value;
this->next=nullptr;
}
}
int main()
Node *Head=nullptr;
int array[]={1,2,3,4,5};
for(int i=0;i<5;i++)
{
if(Head==nullptr)
{
Head=new Node(array[i])
}
else
{
Node *tail=Head;
while(tail->next=!NULL)
{
tail=tail->next;
}
Node *temp;
temp=new Node(array[i]);
tail->next=temp;
}
Node *temp=Head;
while(temp!=nullptr)
{
cout<<temp->value<<" ";
temp=temp->next;
}
}
// this method is also add element but it is add in last but in a good way so decide what you have add.
#include<iostream>
using namespace std;
class Node
{
public:
int value;
Node *next;
Node(int value)
{
this->value = value;
this->next=nullptr;
}
}
int main()
Node *Head,*tail=nullptr;
int array[]={1,2,3,4,5};
for(int i=0;i<5;i++)
{
if(Head==nullptr)
{
Head=new Node(array[i])
tail=Head;
}
else
{
tail->next=new Node(array[i]);
tail=tail->next;
}
Node *temp=Head;
while(temp!=nullptr)
{
cout<<temp->value<<" ";
temp=temp->next;
}
}