SlideShare a Scribd company logo
21. Inheritance in c#
using System;
public class Employee
{
public string FirstName;
public string LastName;
public string Email;
public void FullNamePrint()
{
Console.WriteLine("Name = {0} {1}", FirstName, LastName);
}
}
public class FullTimeEmployee : Employee
{
public float AnnualSalary;
public void Salary()
{
Console.WriteLine("Salary = {0}", AnnualSalary);
}
}
public class PartTimeEmployee : Employee
{
public float Hourlysalary;
}
class Program
{
public static void Main()
{
FullTimeEmployee FTE = new FullTimeEmployee();
FTE.FirstName = "Gourav";
FTE.LastName = "Pant";
FTE.Email = "gourav.pant375@gmail.com";
FTE.AnnualSalary = 2000;
FTE.FullNamePrint();
FTE.Salary();
PartTimeEmployee PTE = new PartTimeEmployee();
PTE.FirstName = "Shrawan";
PTE.LastName = "Chowbey";
PTE.Hourlysalary = 300;
PTE.FullNamePrint();
}
}
21. Inheritance in c#
using System;
class ParentClass
{
public ParentClass()
{
Console.WriteLine("Parent Constructor Called");
}
public ParentClass(string Message)
{
Console.WriteLine(Message);
}
}
class ChildClass : ParentClass //Single level Inheritence
{
public ChildClass() : base("Hello this is another Constructor") //base() method is
use to choose which constructor is called from Parent Class
{
Console.WriteLine("Child Constructor Called");
}
}
class Program
{
public static void Main()
{
ChildClass CC = new ChildClass(); //Parent Class constructor call first before
child class constructor
}
}
23. Polymorphism in C#
using System;
public class Employee
{
public string FirstName = "Gourav";
public string LastName = "Pant";
public virtual void PrintFullName()
{
Console.WriteLine(FirstName + " " + LastName);
}
}
public class PartTimeEmployee : Employee
{
public override void PrintFullName()
{
Console.WriteLine(FirstName + " " + LastName + "- PART TIME");
}
}
public class FullTimeEmployee : Employee
{
public override void PrintFullName()
{
Console.WriteLine(FirstName + " " + LastName + "- FULL TIME");
}
}
public class TemporaryEmployee : Employee //if override function is not there then
virtual funtion is used.
{
//public override void PrintFullName()
//{
// Console.WriteLine(FirstName + " " + LastName + "- TEMPORARY");
//}
}
class Program
{
public static void Main()
{
Employee E1 = new Employee();
E1.PrintFullName();
Employee E2 = new PartTimeEmployee(); // By the help of Parent Class Ref
variable(E2) we can invoke PartTimeEmployee print function during runtime.
E2.PrintFullName();
Employee E3 = new FullTimeEmployee();
E3.PrintFullName();
Employee E4 = new TemporaryEmployee();
E4.PrintFullName();
}
}
26. Why Properties used and use of Getter and Setter methods in C#
using System;
public class Student
{
private int _id;
private string _name;
private int _passMark = 57;
public void SetId(int ID)
{
if (ID <= 0)
{
throw new Exception("Id cannot be less than equal to zero");
}
this._id = ID;
}
public int GetId()
{
return this._id;
}
public void SetName(string Name)
{
if (string.IsNullOrEmpty(Name)) //IsNullOrEmpty is a method of string
class used to check the value is null or empty
{
throw new Exception("Name cannot be Blank or null");
}
this._name = Name;
}
public string GetName()
{
return string.IsNullOrEmpty(this._name) ? "No Name" : this._name; //Ternary
Operator used instead of if else
}
public int GetPassMark()
{
return this._passMark;
}
}
public class Program
{
public static void Main()
{
Student S = new Student();
S.SetId(5);
Console.WriteLine("Id = {0}",S.GetId());
//S.SetName("Gourav"); SetName method is missing so "No Name" should be
return
S.SetName("Gourav");
Console.WriteLine("Name = {0}", S.GetName());
Console.WriteLine("PassMark = {0}", S.GetPassMark()); //PassMark field
should be read only
}
}
27. Properties in C#
using System;
public class Student
{
private int _id;
private string _name;
private int _passMark = 33;
public int ID
{
set
{
if (value <= 0)
{
throw new Exception("Student Id cannot be less than or equal to zero");
}
this._id = value;
}
get
{
return this._id;
}
}
public string Name
{
set
{
if (string.IsNullOrEmpty(value))
{
throw new Exception("Name cannot be Blank or null");
}
this._name = value;
}
get
{
return string.IsNullOrEmpty(this._name) ? "No Name" : this._name;
}
}
public int PassMark
{
get
{
return this._passMark;
}
}
}
public class Program
{
public static void Main()
{
Student C1 = new Student();
C1.ID = 101;
C1.Name = "Gourav"; //Name is missing so "No Name" should be return
Console.WriteLine("Id = {0}", C1.ID);
Console.WriteLine("Name = {0}", C1.Name);
Console.WriteLine("PassMark = {0}", C1.PassMark); //PassMark field
should be read only
}
}
30. Interfaces in C#
using System;
interface Icustomer1
{
void Print1();
}
interface Icustomer2 : Icustomer1 // Icustomer2 Interface inherits from Icustomer1
Interface
{
void Print2();
}
public class Customer : Icustomer2 // Customer Class has to implement the both
interfaces members
{
public void Print1()
{
Console.WriteLine("Print1 Method");
}
public void Print2()
{
Console.WriteLine("Print2 Method");
}
}
public class Program
{
public static void Main()
{
Icustomer2 C1 = new Customer();
C1.Print1();
C1.Print2();
}
}
31. Explicit interfaces implementation
using System;
interface I1
{
void InterfaceMethod();
}
interface I2
{
void InterfaceMethod();
}
public class Program : I1, I2
{
void I1.InterfaceMethod()
{
Console.WriteLine("I1 Interface Method");
}
void I2.InterfaceMethod()
{
Console.WriteLine("I2 Interface Method");
}
public static void Main()
{
/* Program P = new Program();
((I1)P).InterfaceMethod(); // 2 Methods to do explicit interface
implementation. This is typecast method.
((I2)P).InterfaceMethod(); */
I1 i1 = new Program();
i1.InterfaceMethod();
I2 i2 = new Program();
i2.InterfaceMethod();
}
}
Default & Explicit Implementation:-
using System;
interface I1
{
void InterfaceMethod();
}
interface I2
{
void InterfaceMethod();
}
public class Program : I1, I2
{
public void InterfaceMethod() // Implement this method is default
{
Console.WriteLine("I1 Interface Method");
}
void I2.InterfaceMethod() // Implement this method is Explicit
{
Console.WriteLine("I2 Interface Method");
}
public static void Main()
{
Program P = new Program();
P.InterfaceMethod(); // Call the default InterfaceMethod()
((I2)P).InterfaceMethod(); // Call the Explicit InterfaceMethod() which is
of I2 Interface method
}
}
34. Problems of multiple class inheritance
using System;
class A
{
public virtual void print()
{
Console.WriteLine("A Implementation");
}
}
class B : A
{
public override void print()
{
Console.WriteLine("B Implementation");
}
}
class C : A
{
public override void print()
{
Console.WriteLine("C Implementation");
}
}
class D : B, C
{
}
public class Program
{
public static void Main()
{
D d = new D();
d.print(); // So confusion is that which print method should be invoke from
both B and C
}
}
35. Multiple class inheritance using interfaces
using System;
interface IA
{
void AMethod();
}
class A : IA
{
public void AMethod()
{
Console.WriteLine("A Method");
}
}
interface IB
{
void BMethod();
}
class B : IB
{
public void BMethod()
{
Console.WriteLine("B Method");
}
}
class AB : IA, IB
{
A a = new A();
B b = new B();
public void AMethod()
{
a.AMethod();
}
public void BMethod()
{
b.BMethod();
}
}
public class Program
{
public static void Main()
{
AB ab = new AB();
ab.AMethod();
ab.BMethod();
}
}
C# programs

More Related Content

What's hot

C interview question answer 2
C interview question answer 2C interview question answer 2
C interview question answer 2
Amit Kapoor
 
Python programming workshop session 2
Python programming workshop session 2Python programming workshop session 2
Python programming workshop session 2
Abdul Haseeb
 
Functions in C
Functions in CFunctions in C
Functions in C
Princy Nelson
 
Fp201 unit5 1
Fp201 unit5 1Fp201 unit5 1
Fp201 unit5 1
rohassanie
 
Java Programs
Java ProgramsJava Programs
Java Programs
vvpadhu
 
C#
C#C#
Python workshop session 6
Python workshop session 6Python workshop session 6
Python workshop session 6
Abdul Haseeb
 
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Mario Fusco
 
Advance python programming
Advance python programming Advance python programming
Advance python programming
Jagdish Chavan
 
C# 6.0 - April 2014 preview
C# 6.0 - April 2014 previewC# 6.0 - April 2014 preview
C# 6.0 - April 2014 preview
Paulo Morgado
 
C function
C functionC function
C function
thirumalaikumar3
 
OOP and FP - Become a Better Programmer
OOP and FP - Become a Better ProgrammerOOP and FP - Become a Better Programmer
OOP and FP - Become a Better Programmer
Mario Fusco
 
Lecture#9 Arrays in c++
Lecture#9 Arrays in c++Lecture#9 Arrays in c++
Lecture#9 Arrays in c++
NUST Stuff
 
From object oriented to functional domain modeling
From object oriented to functional domain modelingFrom object oriented to functional domain modeling
From object oriented to functional domain modeling
Mario Fusco
 
C# Variables and Operators
C# Variables and OperatorsC# Variables and Operators
C# Variables and Operators
Sunil OS
 
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Abu Saleh
 
SoCal Code Camp 2015: An introduction to Java 8
SoCal Code Camp 2015: An introduction to Java 8SoCal Code Camp 2015: An introduction to Java 8
SoCal Code Camp 2015: An introduction to Java 8
Chaitanya Ganoo
 
Kotlin Perfomance on Android / Александр Смирнов (Splyt)
Kotlin Perfomance on Android / Александр Смирнов (Splyt)Kotlin Perfomance on Android / Александр Смирнов (Splyt)
Kotlin Perfomance on Android / Александр Смирнов (Splyt)
Ontico
 
C# for-java-developers
C# for-java-developersC# for-java-developers
C# for-java-developers
Dhaval Dalal
 
Final JAVA Practical of BCA SEM-5.
Final JAVA Practical of BCA SEM-5.Final JAVA Practical of BCA SEM-5.
Final JAVA Practical of BCA SEM-5.
Nishan Barot
 

What's hot (20)

C interview question answer 2
C interview question answer 2C interview question answer 2
C interview question answer 2
 
Python programming workshop session 2
Python programming workshop session 2Python programming workshop session 2
Python programming workshop session 2
 
Functions in C
Functions in CFunctions in C
Functions in C
 
Fp201 unit5 1
Fp201 unit5 1Fp201 unit5 1
Fp201 unit5 1
 
Java Programs
Java ProgramsJava Programs
Java Programs
 
C#
C#C#
C#
 
Python workshop session 6
Python workshop session 6Python workshop session 6
Python workshop session 6
 
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...
 
Advance python programming
Advance python programming Advance python programming
Advance python programming
 
C# 6.0 - April 2014 preview
C# 6.0 - April 2014 previewC# 6.0 - April 2014 preview
C# 6.0 - April 2014 preview
 
C function
C functionC function
C function
 
OOP and FP - Become a Better Programmer
OOP and FP - Become a Better ProgrammerOOP and FP - Become a Better Programmer
OOP and FP - Become a Better Programmer
 
Lecture#9 Arrays in c++
Lecture#9 Arrays in c++Lecture#9 Arrays in c++
Lecture#9 Arrays in c++
 
From object oriented to functional domain modeling
From object oriented to functional domain modelingFrom object oriented to functional domain modeling
From object oriented to functional domain modeling
 
C# Variables and Operators
C# Variables and OperatorsC# Variables and Operators
C# Variables and Operators
 
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
 
SoCal Code Camp 2015: An introduction to Java 8
SoCal Code Camp 2015: An introduction to Java 8SoCal Code Camp 2015: An introduction to Java 8
SoCal Code Camp 2015: An introduction to Java 8
 
Kotlin Perfomance on Android / Александр Смирнов (Splyt)
Kotlin Perfomance on Android / Александр Смирнов (Splyt)Kotlin Perfomance on Android / Александр Смирнов (Splyt)
Kotlin Perfomance on Android / Александр Смирнов (Splyt)
 
C# for-java-developers
C# for-java-developersC# for-java-developers
C# for-java-developers
 
Final JAVA Practical of BCA SEM-5.
Final JAVA Practical of BCA SEM-5.Final JAVA Practical of BCA SEM-5.
Final JAVA Practical of BCA SEM-5.
 

Similar to C# programs

Object Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ ExamsObject Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ Exams
MuhammadTalha436
 
Advanced Java - Practical File
Advanced Java - Practical FileAdvanced Java - Practical File
Advanced Java - Practical File
Fahad Shaikh
 
@author public class Person{   String sname, .pdf
  @author   public class Person{   String sname, .pdf  @author   public class Person{   String sname, .pdf
@author public class Person{   String sname, .pdf
aplolomedicalstoremr
 
Java programs
Java programsJava programs
Java practical
Java practicalJava practical
Java practical
shweta-sharma99
 
Java Class Design
Java Class DesignJava Class Design
Java Class Design
Ganesh Samarthyam
 
Thread
ThreadThread
Thread
phanleson
 
39927902 c-labmanual
39927902 c-labmanual39927902 c-labmanual
39927902 c-labmanual
Srinivasa Babji Josyula
 
39927902 c-labmanual
39927902 c-labmanual39927902 c-labmanual
39927902 c-labmanual
Srinivasa Babji Josyula
 
2. Create a Java class called EmployeeMain within the same project Pr.docx
 2. Create a Java class called EmployeeMain within the same project Pr.docx 2. Create a Java class called EmployeeMain within the same project Pr.docx
2. Create a Java class called EmployeeMain within the same project Pr.docx
ajoy21
 
Algoritmos sujei
Algoritmos sujeiAlgoritmos sujei
Algoritmos sujei
gersonjack
 
Class 6 2ciclo
Class 6 2cicloClass 6 2ciclo
Class 6 2ciclo
Carlos Alcivar
 
C# labprograms
C# labprogramsC# labprograms
C# labprograms
Jafar Nesargi
 
Java practical
Java practicalJava practical
Java practical
william otto
 
Integration Project Inspection 3
Integration Project Inspection 3Integration Project Inspection 3
Integration Project Inspection 3
Dillon Lee
 
Pratik Bakane C++
Pratik Bakane C++Pratik Bakane C++
Pratik Bakane C++
pratikbakane
 
TechTalk - Dotnet
TechTalk - DotnetTechTalk - Dotnet
TechTalk - Dotnet
heinrich.wendel
 
Inheritance and-polymorphism
Inheritance and-polymorphismInheritance and-polymorphism
Inheritance and-polymorphism
Usama Malik
 
C# programs
C# programsC# programs
C# programs
Syed Mustafa
 
Java interface
Java interfaceJava interface
Java interface
Md. Tanvir Hossain
 

Similar to C# programs (20)

Object Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ ExamsObject Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ Exams
 
Advanced Java - Practical File
Advanced Java - Practical FileAdvanced Java - Practical File
Advanced Java - Practical File
 
@author public class Person{   String sname, .pdf
  @author   public class Person{   String sname, .pdf  @author   public class Person{   String sname, .pdf
@author public class Person{   String sname, .pdf
 
Java programs
Java programsJava programs
Java programs
 
Java practical
Java practicalJava practical
Java practical
 
Java Class Design
Java Class DesignJava Class Design
Java Class Design
 
Thread
ThreadThread
Thread
 
39927902 c-labmanual
39927902 c-labmanual39927902 c-labmanual
39927902 c-labmanual
 
39927902 c-labmanual
39927902 c-labmanual39927902 c-labmanual
39927902 c-labmanual
 
2. Create a Java class called EmployeeMain within the same project Pr.docx
 2. Create a Java class called EmployeeMain within the same project Pr.docx 2. Create a Java class called EmployeeMain within the same project Pr.docx
2. Create a Java class called EmployeeMain within the same project Pr.docx
 
Algoritmos sujei
Algoritmos sujeiAlgoritmos sujei
Algoritmos sujei
 
Class 6 2ciclo
Class 6 2cicloClass 6 2ciclo
Class 6 2ciclo
 
C# labprograms
C# labprogramsC# labprograms
C# labprograms
 
Java practical
Java practicalJava practical
Java practical
 
Integration Project Inspection 3
Integration Project Inspection 3Integration Project Inspection 3
Integration Project Inspection 3
 
Pratik Bakane C++
Pratik Bakane C++Pratik Bakane C++
Pratik Bakane C++
 
TechTalk - Dotnet
TechTalk - DotnetTechTalk - Dotnet
TechTalk - Dotnet
 
Inheritance and-polymorphism
Inheritance and-polymorphismInheritance and-polymorphism
Inheritance and-polymorphism
 
C# programs
C# programsC# programs
C# programs
 
Java interface
Java interfaceJava interface
Java interface
 

Recently uploaded

A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of CodeA Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
Aftab Hussain
 
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdfVitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
Drona Infotech
 
Using Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional SafetyUsing Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional Safety
Ayan Halder
 
Requirement Traceability in Xen Functional Safety
Requirement Traceability in Xen Functional SafetyRequirement Traceability in Xen Functional Safety
Requirement Traceability in Xen Functional Safety
Ayan Halder
 
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdfAutomated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
timtebeek1
 
SMS API Integration in Saudi Arabia| Best SMS API Service
SMS API Integration in Saudi Arabia| Best SMS API ServiceSMS API Integration in Saudi Arabia| Best SMS API Service
SMS API Integration in Saudi Arabia| Best SMS API Service
Yara Milbes
 
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Crescat
 
Graspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code AnalysisGraspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code Analysis
Aftab Hussain
 
UI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design SystemUI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design System
Peter Muessig
 
What is Master Data Management by PiLog Group
What is Master Data Management by PiLog GroupWhat is Master Data Management by PiLog Group
What is Master Data Management by PiLog Group
aymanquadri279
 
Webinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for EmbeddedWebinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for Embedded
ICS
 
How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?
ToXSL Technologies
 
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
XfilesPro
 
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissancesAtelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Neo4j
 
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
kalichargn70th171
 
GreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-JurisicGreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-Jurisic
Green Software Development
 
Hand Rolled Applicative User Validation Code Kata
Hand Rolled Applicative User ValidationCode KataHand Rolled Applicative User ValidationCode Kata
Hand Rolled Applicative User Validation Code Kata
Philip Schwarz
 
socradar-q1-2024-aviation-industry-report.pdf
socradar-q1-2024-aviation-industry-report.pdfsocradar-q1-2024-aviation-industry-report.pdf
socradar-q1-2024-aviation-industry-report.pdf
SOCRadar
 
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling ExtensionsUI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
Peter Muessig
 

Recently uploaded (20)

A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of CodeA Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
 
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdfVitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdf
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
 
Using Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional SafetyUsing Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional Safety
 
Requirement Traceability in Xen Functional Safety
Requirement Traceability in Xen Functional SafetyRequirement Traceability in Xen Functional Safety
Requirement Traceability in Xen Functional Safety
 
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdfAutomated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
 
SMS API Integration in Saudi Arabia| Best SMS API Service
SMS API Integration in Saudi Arabia| Best SMS API ServiceSMS API Integration in Saudi Arabia| Best SMS API Service
SMS API Integration in Saudi Arabia| Best SMS API Service
 
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
 
Graspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code AnalysisGraspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code Analysis
 
UI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design SystemUI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design System
 
What is Master Data Management by PiLog Group
What is Master Data Management by PiLog GroupWhat is Master Data Management by PiLog Group
What is Master Data Management by PiLog Group
 
Webinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for EmbeddedWebinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for Embedded
 
How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?
 
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
 
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissancesAtelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissances
 
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
 
GreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-JurisicGreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-Jurisic
 
Hand Rolled Applicative User Validation Code Kata
Hand Rolled Applicative User ValidationCode KataHand Rolled Applicative User ValidationCode Kata
Hand Rolled Applicative User Validation Code Kata
 
socradar-q1-2024-aviation-industry-report.pdf
socradar-q1-2024-aviation-industry-report.pdfsocradar-q1-2024-aviation-industry-report.pdf
socradar-q1-2024-aviation-industry-report.pdf
 
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling ExtensionsUI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
 

C# programs

  • 1. 21. Inheritance in c# using System; public class Employee { public string FirstName; public string LastName; public string Email; public void FullNamePrint() { Console.WriteLine("Name = {0} {1}", FirstName, LastName); } } public class FullTimeEmployee : Employee { public float AnnualSalary; public void Salary() { Console.WriteLine("Salary = {0}", AnnualSalary); } } public class PartTimeEmployee : Employee { public float Hourlysalary; } class Program { public static void Main() { FullTimeEmployee FTE = new FullTimeEmployee(); FTE.FirstName = "Gourav"; FTE.LastName = "Pant"; FTE.Email = "gourav.pant375@gmail.com"; FTE.AnnualSalary = 2000; FTE.FullNamePrint(); FTE.Salary(); PartTimeEmployee PTE = new PartTimeEmployee(); PTE.FirstName = "Shrawan"; PTE.LastName = "Chowbey";
  • 2. PTE.Hourlysalary = 300; PTE.FullNamePrint(); } } 21. Inheritance in c# using System; class ParentClass { public ParentClass() { Console.WriteLine("Parent Constructor Called"); } public ParentClass(string Message) { Console.WriteLine(Message); } } class ChildClass : ParentClass //Single level Inheritence { public ChildClass() : base("Hello this is another Constructor") //base() method is use to choose which constructor is called from Parent Class { Console.WriteLine("Child Constructor Called"); } } class Program { public static void Main() { ChildClass CC = new ChildClass(); //Parent Class constructor call first before child class constructor } } 23. Polymorphism in C#
  • 3. using System; public class Employee { public string FirstName = "Gourav"; public string LastName = "Pant"; public virtual void PrintFullName() { Console.WriteLine(FirstName + " " + LastName); } } public class PartTimeEmployee : Employee { public override void PrintFullName() { Console.WriteLine(FirstName + " " + LastName + "- PART TIME"); } } public class FullTimeEmployee : Employee { public override void PrintFullName() { Console.WriteLine(FirstName + " " + LastName + "- FULL TIME"); } } public class TemporaryEmployee : Employee //if override function is not there then virtual funtion is used. { //public override void PrintFullName() //{ // Console.WriteLine(FirstName + " " + LastName + "- TEMPORARY"); //} } class Program { public static void Main() { Employee E1 = new Employee(); E1.PrintFullName(); Employee E2 = new PartTimeEmployee(); // By the help of Parent Class Ref variable(E2) we can invoke PartTimeEmployee print function during runtime. E2.PrintFullName(); Employee E3 = new FullTimeEmployee(); E3.PrintFullName(); Employee E4 = new TemporaryEmployee(); E4.PrintFullName(); }
  • 4. } 26. Why Properties used and use of Getter and Setter methods in C# using System; public class Student { private int _id; private string _name; private int _passMark = 57; public void SetId(int ID) { if (ID <= 0) { throw new Exception("Id cannot be less than equal to zero"); } this._id = ID; } public int GetId() { return this._id; } public void SetName(string Name) { if (string.IsNullOrEmpty(Name)) //IsNullOrEmpty is a method of string class used to check the value is null or empty { throw new Exception("Name cannot be Blank or null"); } this._name = Name; } public string GetName() { return string.IsNullOrEmpty(this._name) ? "No Name" : this._name; //Ternary Operator used instead of if else } public int GetPassMark() { return this._passMark; } } public class Program {
  • 5. public static void Main() { Student S = new Student(); S.SetId(5); Console.WriteLine("Id = {0}",S.GetId()); //S.SetName("Gourav"); SetName method is missing so "No Name" should be return S.SetName("Gourav"); Console.WriteLine("Name = {0}", S.GetName()); Console.WriteLine("PassMark = {0}", S.GetPassMark()); //PassMark field should be read only } } 27. Properties in C# using System; public class Student { private int _id; private string _name; private int _passMark = 33; public int ID { set { if (value <= 0) { throw new Exception("Student Id cannot be less than or equal to zero"); } this._id = value; } get { return this._id; } } public string Name { set { if (string.IsNullOrEmpty(value)) { throw new Exception("Name cannot be Blank or null"); }
  • 6. this._name = value; } get { return string.IsNullOrEmpty(this._name) ? "No Name" : this._name; } } public int PassMark { get { return this._passMark; } } } public class Program { public static void Main() { Student C1 = new Student(); C1.ID = 101; C1.Name = "Gourav"; //Name is missing so "No Name" should be return Console.WriteLine("Id = {0}", C1.ID); Console.WriteLine("Name = {0}", C1.Name); Console.WriteLine("PassMark = {0}", C1.PassMark); //PassMark field should be read only } } 30. Interfaces in C# using System; interface Icustomer1 { void Print1(); } interface Icustomer2 : Icustomer1 // Icustomer2 Interface inherits from Icustomer1 Interface { void Print2(); }
  • 7. public class Customer : Icustomer2 // Customer Class has to implement the both interfaces members { public void Print1() { Console.WriteLine("Print1 Method"); } public void Print2() { Console.WriteLine("Print2 Method"); } } public class Program { public static void Main() { Icustomer2 C1 = new Customer(); C1.Print1(); C1.Print2(); } } 31. Explicit interfaces implementation using System; interface I1 { void InterfaceMethod(); } interface I2 { void InterfaceMethod(); } public class Program : I1, I2 { void I1.InterfaceMethod() { Console.WriteLine("I1 Interface Method"); }
  • 8. void I2.InterfaceMethod() { Console.WriteLine("I2 Interface Method"); } public static void Main() { /* Program P = new Program(); ((I1)P).InterfaceMethod(); // 2 Methods to do explicit interface implementation. This is typecast method. ((I2)P).InterfaceMethod(); */ I1 i1 = new Program(); i1.InterfaceMethod(); I2 i2 = new Program(); i2.InterfaceMethod(); } } Default & Explicit Implementation:- using System; interface I1 { void InterfaceMethod(); } interface I2 { void InterfaceMethod(); } public class Program : I1, I2 { public void InterfaceMethod() // Implement this method is default { Console.WriteLine("I1 Interface Method"); } void I2.InterfaceMethod() // Implement this method is Explicit { Console.WriteLine("I2 Interface Method"); } public static void Main() {
  • 9. Program P = new Program(); P.InterfaceMethod(); // Call the default InterfaceMethod() ((I2)P).InterfaceMethod(); // Call the Explicit InterfaceMethod() which is of I2 Interface method } } 34. Problems of multiple class inheritance using System; class A { public virtual void print() { Console.WriteLine("A Implementation"); } } class B : A { public override void print() { Console.WriteLine("B Implementation"); } } class C : A { public override void print() { Console.WriteLine("C Implementation"); } } class D : B, C { } public class Program { public static void Main() { D d = new D(); d.print(); // So confusion is that which print method should be invoke from both B and C } }
  • 10. 35. Multiple class inheritance using interfaces using System; interface IA { void AMethod(); } class A : IA { public void AMethod() { Console.WriteLine("A Method"); } } interface IB { void BMethod(); } class B : IB { public void BMethod() { Console.WriteLine("B Method"); } } class AB : IA, IB { A a = new A(); B b = new B(); public void AMethod() { a.AMethod(); } public void BMethod() { b.BMethod(); } } public class Program { public static void Main() { AB ab = new AB(); ab.AMethod(); ab.BMethod(); } }