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

C# Notes

Uploaded by

1122.awaissaleem
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

C# Notes

Uploaded by

1122.awaissaleem
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Class and Objects

Class is like a container that have store a data that and contain and related method.

How can Static method access in main class?


using Operation;
using System;

namespace Helloworld
{
public class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World");

Class1.test2(); // access with class name and method name


}
}
}

// other class

using System;

namespace Operation
{

public class Class1


{
public static void test2()
{
Console.WriteLine("Enter your First_number:");
int first_number =Convert.ToInt16(Console.ReadLine());
Console.WriteLine("Enter your last Number:");
int last number =Convert.ToInt16(Console.ReadLine());
int result = first_number + last number;
Console.WriteLine("Result of number:"+ result);
Console.ReadLine();
}
}
}

Now in this code you have to see that a static method have to access in to main file.

How can Method Access by Object without static bana?


using Operation;
using System;

namespace Helloworld
{
public class Program
{
static void Main (string [] args)
{
Console.WriteLine("Hello World");

Calculator calculator = new Calculator (); //OBJECT


calculator.num1 = 10;
//with the help of object value bi da sakta Hain agar public ho method
//calculator.num2 = 20;
calculator. Add ();

}
}
}

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

}
}

// this method you have to access with the help of object

How can return function will be define and and how can access?

using System;

namespace Operation
{
public class Calculator
{

public int Add(int num1,int num2) // return method will be define


{
int result;
result = num1 + num2;
Display(result);
return result;

}
//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();
}

}
}

In main they have access

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

}
}
}

using System;// Important Program

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

static int First500prime()


{
int number = 2;
int sum = 0;
int count = 0;
while (count < 500)
{
if (Isprime(number))
{
sum += number;
count++;
}
number++;
}
return sum;
}

static bool Isprime(int num)


{
if (num <= 1) return false;
for (int i = 2; i <= Math.Sqrt(num); i++)
{
if (num % i == 0)
{
return false;
}
}
return true;
}
}
}

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

class vivo : mobile


{
public override void specifications()
{
console.writeline("vivo specifications: snapdragon 888, 8gb ram, 128gb storage");
}
}

class samsung : mobile


{
public override void specifications()
{
console.writeline("samsung specifications: exynos 2100, 12gb ram, 256gb storage");
}
}

class techno : mobile


{
public override void specifications()
{
console.writeline("techno specifications: mediatek helio g95, 6gb ram, 128gb storage");
}
}
public static void main(string[] args)
{
console.writeline("enter the name of the mobile brand (vivo, samsung, techno) or 'exit' to quit:");
string input = console.readline().tolower();
mobile mobile = null;

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.

Interface is like a polymorphsim

Car Tesla =new ElectriacCar();

Car is polymorphsim: Tesla is name of object : new ElectricCar() is a name of object.


BaseKeyword in C#:
Use base keyword not super use In java:

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

Encapsulation cp=new Encapsulation();


cp.setter(33);
cp.getter();
}

}
class Encapsulation
{

private int accountBalance = 0;

public void setter(int value)


{

accountBalance = value;

}
public void getter()
{
Console.WriteLine(accountBalance);
}

C# Properties // always have return not use void


using Operation;
using System;
using System.IO;
using System.Linq;

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

Encapsulation cp=new Encapsulation();


cp.Balance=2000;
Console.WriteLine(cp.Balance);

}
class Encapsulation
{

private int accountBalance = 0;

public int Balance


{
set
{
accountBalance = value;
}
get
{
return accountBalance;
}
}

}
}

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.

foreach (Vechile vc in vechile)


{
vc.Goto(); }

Other Example
var carobje = new Car();

// carobje = GetCar("Honda");
// carobje = GetCar("Mehran");

public static Car GetCar(string carType)


// {
// if(carType == "Honda")
// {
// return new Honda();

// } else if (carType == "Mehran")


// {
// return new Mehran();
// }
// else
// {
// return new Car();
// }
// }

Other example of polymorphsim:


namespace helloworld
{
class progrmmer
{

class mobile
{
public virtual void specifications()
{
console.writeline("mobile specifications");
}
}

class vivo : mobile


{
public override void specifications()
{
console.writeline("vivo specifications: snapdragon 888, 8gb ram, 128gb storage");
}
}

class samsung : mobile


{
public override void specifications()
{
console.writeline("samsung specifications: exynos 2100, 12gb ram, 256gb storage");
}
}

class techno : mobile


{
public override void specifications()
{
console.writeline("techno specifications: mediatek helio g95, 6gb ram, 128gb storage");
}
}
public static void main(string[] args)
{
console.writeline("enter the name of the mobile brand (vivo, samsung, techno) or 'exit' to quit:");
string input = console.readline().tolower();

mobile mobile = null;

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>

using namespace std;

void Sortarray(int array[],int size)

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;

bool UniqueNumber(int array[],int size)

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

cout<<array[i]<<" number is "<<count<<" repeated"<<endl;

for (int i = 0; i < tempsize; i++) {

for (int j = i + 1; j < tempsize; j++) {

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

int size = sizeof(array) / sizeof(array[0]); // Calculate the size of the array

cout<<UniqueNumber(array, size); // Pass the correct size of the array


return 0;

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;

}
}

//printing the values


Node *temp=Head;
while(temp!=NULL)
{
cout<<temp->value<<" ";
temp=temp->next;
}

// in this code add node in last element


#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=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;
}
}

You might also like