SlideShare a Scribd company logo
Articles from Jinal Desai .NET
OOPS With CSharp
2012-11-10 13:11:59 Jinal Desai

Some scenarios I came across while working with C# where OOPs funda deviated
at some level to provide more flexibility. Suggest me some more scenarios if I miss
anyone.

Scenario # 1.
Two interface having same method signature is derived by a class.

How to implement methods in a class with same signature from different
interfaces?
We can use named identifiers to identify the common method we implemented
based on two different interfaces.

i.e. If there is two interfaces i1 and i2, both has one method with same signature
(void test(int a)), then at the time of implementing these methods inside class “abc”
we can use i1.test and i2.test to differentiate the implementation of methods from
two different interfaces having same signature.

Interface i1
{
  void test(int a);
}

interface i2
{
  void test(int a);
}

public class abc: i1, i2
{
  void i1.test(int a)
  {
    Console.WriteLine(“i1 implementation: “ + a);
  }

    void i2.test(int a)
    {
      Console.WriteLine(“i2 implementation: “ + a);
    }
}

Note : The only thing that can be noted here is that at the time of implementing test()
methods from two different interfaces in a single class, the public modifier is not
required. If you define public modifier for the implementation of test() method in
class “abc” then it will give you error “The modifier ‘public’ is not valid for this item”.

How to call those implemented methods from another class?
To access methods implemented in a class “abc” into another class, we need to
use respected interface rather than class “abc”.

i.e. If I need to use method of interface i1 then I need to do code as follow.

i1 testi1=new abc();
i1.test(10);

Above code will call test() method of interface i1. In the similar manner we can call
test() method of interface i2. If we need to call both the method with one instance of
class “abc” then we need to cast “abc” class into an interface of which method we
are intended to call as demonstrated below.

abc testabc=new abc();
(testabc as i1).test(10);
(testabc as i2).test(10);

What if I has a method inside class “abc” with same signature prototyped in
interface i1 and i2 (those interfaces we have implemented in class “abc”)?
In that case we can call the method in a normal way with the instance of class
“abc”.
i.e.

public class abc: i1, i2
{
  void i1.test(int a)
  {
    Console.WriteLine(“i1 implementation: “ + a);
  }

 void i2.test(int a)
 {
   Console.WriteLine(“i2 implementation: “ + a);
 }

 void test(int a)
 {
   Console.WriteLine(“Normal method inside the class: ” + a);
 }
}
//To call the method normally.
abc testabc=new abc();
testabc.test(10);

How we can access implemented method of i1 from inside the implemented
method of i1, in a class “abc” or vice-versa?
It’s simple. We need to cast “this” into respected interface to use it’s method.
i.e.
public class abc: i1, i2
{
  void i1.test(int a)
  {
    Console.WriteLine(“i1 implementation: “ + a);
  }

    void i2.test(int a)
    {
      Console.WriteLine(“i2 implementation: “ + a);
      (this as i1).test(a + 20);
    }
}

Scenario #2
Which are the things allowed inside an interface in case of C#? (In other way
can automated property/event/delegate…etc allowed inside an interface
while using C#.NET?)

First thing first is method signature can be declared in an interface, it is the purpose
of an interface. Except method signature, property declaration and event declaration
are also allowed inside an interface.

Property Declaration inside an Interface

interface Itest
{
  int a { get; set; }
  int b { get; set; }
}

The only thing you need to care here is that, you need to implement these properties
in an implemented class. Compiler will not understand these declared properties as
automated properties. In a class if we write properties like this then it should be
considered as an automated properties, implementation is not required in that case.
i.e. Implementation of properties defined in Itest
class Test:ITest
{
  int aVar = 0;
  int bVar = 0;
  public int a
  {
    get
    {
return aVar;
     }
     set
     {
       aVar = value;
     }
    }
    public int b
    {
      get
      {
        return bVar;
      }
      set
      {
        bVar = value;
      }
    }
}

Event Declaration inside an Interface

public delegate void ChangedEventHandler
(object sender, EventArgs e);
interface Itest
{
  event ChangedEventHandler Changed;
}

Now to use this event inside the implemented class, following is an example.

Class Test: Itest
{
  public void BindEvent()
  {
    if(ChangedEventHandler!=null)
     Changed+=new ChangedEventHandler(Test_Changed);
  }
  void Test_Changed(object sender, EventArgs e)
  {
    //Event Fired
  }
}

This way you can confirm that all the implementer classes implemented the event to
become sync with the interface design. So that all the classes can be called in a
similar pattern designed by an interface.

Scenario #3
I have one interface Itest as defined below.

Interface Itest
{
  int sum(int a, int b);
}

I have implemented the interface into Calculator class as follow.

Class Calculator:ITest
{
  public int sum(int a, int b)
  {
    return (a+b);
  }
}

I have one more class named ScientificCalculator derived from Calculator class
having same method with same signature.

Class ScientificCalculator:Calculator
{
  public int sum(int a, int b)
  {
    return base.sum(a,b);
  }
}

Can it be possible to access sum method of calculator class if I created instance of
ScientificCalculator class by assigning it to interface Itest?.
i.e.
Itest itest = new ScientificCalculator();

Now is it possible to access sum method of Calculator class through object itest?
Yes, we can access it but for that we need to cast itest object into Calculator class.

((Calculator)itest).Sum(20, 30);

If you access directly sum method then it obviously refer sum method of class
ScientificCalculator.

More Related Content

What's hot

Intake 37 4
Intake 37 4Intake 37 4
Intake 37 4
Mahmoud Ouf
 
Lambda Expressions in C# From Beginner To Expert - Jaliya Udagedara
Lambda Expressions in C# From Beginner To Expert - Jaliya UdagedaraLambda Expressions in C# From Beginner To Expert - Jaliya Udagedara
Lambda Expressions in C# From Beginner To Expert - Jaliya UdagedaraJaliya Udagedara
 
Test driven development
Test driven developmentTest driven development
Test driven development
Xebia India
 
Intake 37 5
Intake 37 5Intake 37 5
Intake 37 5
Mahmoud Ouf
 
Java adapter
Java adapterJava adapter
Java adapter
Arati Gadgil
 
15review(abstract classandinterfaces)
15review(abstract classandinterfaces)15review(abstract classandinterfaces)
15review(abstract classandinterfaces)
IIUM
 
Operators used in vb.net
Operators used in vb.netOperators used in vb.net
Operators used in vb.net
Jaya Kumari
 
Operators in java
Operators in javaOperators in java
Operators in java
Madishetty Prathibha
 
Learn Java Part 2
Learn Java Part 2Learn Java Part 2
Learn Java Part 2
Gurpreet singh
 
Introduction of C# BY Adarsh Singh
Introduction of C# BY Adarsh SinghIntroduction of C# BY Adarsh Singh
Introduction of C# BY Adarsh Singh
singhadarsh
 
Odersky week1 notes
Odersky week1 notesOdersky week1 notes
Odersky week1 notes
Doug Chang
 
Introduction to Matlab Scripts
Introduction to Matlab ScriptsIntroduction to Matlab Scripts
Introduction to Matlab Scripts
Shameer Ahmed Koya
 
Templates exception handling
Templates exception handlingTemplates exception handling
Templates exception handling
sanya6900
 
Experiment 9(exceptions)
Experiment 9(exceptions)Experiment 9(exceptions)
Experiment 9(exceptions)
ShivamKumar682885
 
Handling inputs via scanner class
Handling inputs via scanner classHandling inputs via scanner class
Handling inputs via scanner class
simarsimmygrewal
 
C# - Part 1
C# - Part 1C# - Part 1
C# - Part 1
Md. Mahedee Hasan
 
Inheritance
InheritanceInheritance
Inheritance
Jaya Kumari
 
Storage Class Specifiers in C++
Storage Class Specifiers in C++Storage Class Specifiers in C++
Storage Class Specifiers in C++
Reddhi Basu
 

What's hot (20)

Intake 37 4
Intake 37 4Intake 37 4
Intake 37 4
 
Presentation
PresentationPresentation
Presentation
 
Lambda Expressions in C# From Beginner To Expert - Jaliya Udagedara
Lambda Expressions in C# From Beginner To Expert - Jaliya UdagedaraLambda Expressions in C# From Beginner To Expert - Jaliya Udagedara
Lambda Expressions in C# From Beginner To Expert - Jaliya Udagedara
 
Test driven development
Test driven developmentTest driven development
Test driven development
 
Intake 37 5
Intake 37 5Intake 37 5
Intake 37 5
 
Java adapter
Java adapterJava adapter
Java adapter
 
15review(abstract classandinterfaces)
15review(abstract classandinterfaces)15review(abstract classandinterfaces)
15review(abstract classandinterfaces)
 
Operators used in vb.net
Operators used in vb.netOperators used in vb.net
Operators used in vb.net
 
Operators in java
Operators in javaOperators in java
Operators in java
 
Learn Java Part 2
Learn Java Part 2Learn Java Part 2
Learn Java Part 2
 
Introduction of C# BY Adarsh Singh
Introduction of C# BY Adarsh SinghIntroduction of C# BY Adarsh Singh
Introduction of C# BY Adarsh Singh
 
Odersky week1 notes
Odersky week1 notesOdersky week1 notes
Odersky week1 notes
 
Introduction to Matlab Scripts
Introduction to Matlab ScriptsIntroduction to Matlab Scripts
Introduction to Matlab Scripts
 
Templates exception handling
Templates exception handlingTemplates exception handling
Templates exception handling
 
Experiment 9(exceptions)
Experiment 9(exceptions)Experiment 9(exceptions)
Experiment 9(exceptions)
 
Functions
FunctionsFunctions
Functions
 
Handling inputs via scanner class
Handling inputs via scanner classHandling inputs via scanner class
Handling inputs via scanner class
 
C# - Part 1
C# - Part 1C# - Part 1
C# - Part 1
 
Inheritance
InheritanceInheritance
Inheritance
 
Storage Class Specifiers in C++
Storage Class Specifiers in C++Storage Class Specifiers in C++
Storage Class Specifiers in C++
 

Viewers also liked

Mvc interview questions – deep dive jinal desai
Mvc interview questions – deep dive   jinal desaiMvc interview questions – deep dive   jinal desai
Mvc interview questions – deep dive jinal desai
jinaldesailive
 
200+sql server interview_questions
200+sql server interview_questions200+sql server interview_questions
200+sql server interview_questions
Mahesh Gupta (DBATAG) - SQL Server Consultant
 
Top 100 .Net Interview Questions and Answer
Top 100 .Net Interview Questions and AnswerTop 100 .Net Interview Questions and Answer
Top 100 .Net Interview Questions and Answer
Vineet Kumar Saini
 
MS.Net Interview Questions - Simplified
MS.Net Interview Questions - SimplifiedMS.Net Interview Questions - Simplified
MS.Net Interview Questions - Simplified
Mohd Manzoor Ahmed
 
Architecture of .net framework
Architecture of .net frameworkArchitecture of .net framework
Architecture of .net framework
Then Murugeshwari
 

Viewers also liked (6)

Mvc interview questions – deep dive jinal desai
Mvc interview questions – deep dive   jinal desaiMvc interview questions – deep dive   jinal desai
Mvc interview questions – deep dive jinal desai
 
200+sql server interview_questions
200+sql server interview_questions200+sql server interview_questions
200+sql server interview_questions
 
Top 100 .Net Interview Questions and Answer
Top 100 .Net Interview Questions and AnswerTop 100 .Net Interview Questions and Answer
Top 100 .Net Interview Questions and Answer
 
MS.Net Interview Questions - Simplified
MS.Net Interview Questions - SimplifiedMS.Net Interview Questions - Simplified
MS.Net Interview Questions - Simplified
 
Architecture of .net framework
Architecture of .net frameworkArchitecture of .net framework
Architecture of .net framework
 
Dotnet basics
Dotnet basicsDotnet basics
Dotnet basics
 

Similar to OOPS With CSharp - Jinal Desai .NET

Interfaces c#
Interfaces c#Interfaces c#
Interfaces c#
Nipam Medhi
 
ECET 370 Exceptional Education - snaptutorial.com
ECET 370 Exceptional Education - snaptutorial.com ECET 370 Exceptional Education - snaptutorial.com
ECET 370 Exceptional Education - snaptutorial.com
donaldzs157
 
Indicate whether each of the following statements is true or false.docx
Indicate whether each of the following statements is true or false.docxIndicate whether each of the following statements is true or false.docx
Indicate whether each of the following statements is true or false.docx
migdalialyle
 
Java 8 features
Java 8 featuresJava 8 features
Java 8 features
Maýur Chourasiya
 
Ecet 370 Education Organization -- snaptutorial.com
Ecet 370   Education Organization -- snaptutorial.comEcet 370   Education Organization -- snaptutorial.com
Ecet 370 Education Organization -- snaptutorial.com
DavisMurphyB81
 
Java interface
Java interfaceJava interface
Java interface
Arati Gadgil
 
Interface
InterfaceInterface
Interface
kamal kotecha
 
Java interface
Java interfaceJava interface
Java interface
Md. Tanvir Hossain
 
Session 6_Java Interfaces_Details_Programs.pdf
Session 6_Java Interfaces_Details_Programs.pdfSession 6_Java Interfaces_Details_Programs.pdf
Session 6_Java Interfaces_Details_Programs.pdf
TabassumMaktum
 
Session 6_Interfaces in va examples .ppt
Session 6_Interfaces in va examples .pptSession 6_Interfaces in va examples .ppt
Session 6_Interfaces in va examples .ppt
TabassumMaktum
 
Session 6_Interfaces in va examples .ppt
Session 6_Interfaces in va examples .pptSession 6_Interfaces in va examples .ppt
Session 6_Interfaces in va examples .ppt
TabassumMaktum
 
Interface
InterfaceInterface
Interface
vvpadhu
 
Still Comparing "this" Pointer to Null?
Still Comparing "this" Pointer to Null?Still Comparing "this" Pointer to Null?
Still Comparing "this" Pointer to Null?
Andrey Karpov
 
MODULE_3_Methods and Classes Overloading.pptx
MODULE_3_Methods and Classes Overloading.pptxMODULE_3_Methods and Classes Overloading.pptx
MODULE_3_Methods and Classes Overloading.pptx
VeerannaKotagi1
 
Jdk1.5 Features
Jdk1.5 FeaturesJdk1.5 Features
Jdk1.5 Features
india_mani
 
C++ Interview Question And Answer
C++ Interview Question And AnswerC++ Interview Question And Answer
C++ Interview Question And Answer
Jagan Mohan Bishoyi
 
C++ questions And Answer
C++ questions And AnswerC++ questions And Answer
C++ questions And Answer
lavparmar007
 

Similar to OOPS With CSharp - Jinal Desai .NET (20)

Interfaces c#
Interfaces c#Interfaces c#
Interfaces c#
 
ECET 370 Exceptional Education - snaptutorial.com
ECET 370 Exceptional Education - snaptutorial.com ECET 370 Exceptional Education - snaptutorial.com
ECET 370 Exceptional Education - snaptutorial.com
 
Indicate whether each of the following statements is true or false.docx
Indicate whether each of the following statements is true or false.docxIndicate whether each of the following statements is true or false.docx
Indicate whether each of the following statements is true or false.docx
 
Java 8 features
Java 8 featuresJava 8 features
Java 8 features
 
Ecet 370 Education Organization -- snaptutorial.com
Ecet 370   Education Organization -- snaptutorial.comEcet 370   Education Organization -- snaptutorial.com
Ecet 370 Education Organization -- snaptutorial.com
 
Java interface
Java interfaceJava interface
Java interface
 
Exception
ExceptionException
Exception
 
Interface
InterfaceInterface
Interface
 
Java interface
Java interfaceJava interface
Java interface
 
Notes on c++
Notes on c++Notes on c++
Notes on c++
 
Session 6_Java Interfaces_Details_Programs.pdf
Session 6_Java Interfaces_Details_Programs.pdfSession 6_Java Interfaces_Details_Programs.pdf
Session 6_Java Interfaces_Details_Programs.pdf
 
Session 6_Interfaces in va examples .ppt
Session 6_Interfaces in va examples .pptSession 6_Interfaces in va examples .ppt
Session 6_Interfaces in va examples .ppt
 
Session 6_Interfaces in va examples .ppt
Session 6_Interfaces in va examples .pptSession 6_Interfaces in va examples .ppt
Session 6_Interfaces in va examples .ppt
 
Java 2
Java   2Java   2
Java 2
 
Interface
InterfaceInterface
Interface
 
Still Comparing "this" Pointer to Null?
Still Comparing "this" Pointer to Null?Still Comparing "this" Pointer to Null?
Still Comparing "this" Pointer to Null?
 
MODULE_3_Methods and Classes Overloading.pptx
MODULE_3_Methods and Classes Overloading.pptxMODULE_3_Methods and Classes Overloading.pptx
MODULE_3_Methods and Classes Overloading.pptx
 
Jdk1.5 Features
Jdk1.5 FeaturesJdk1.5 Features
Jdk1.5 Features
 
C++ Interview Question And Answer
C++ Interview Question And AnswerC++ Interview Question And Answer
C++ Interview Question And Answer
 
C++ questions And Answer
C++ questions And AnswerC++ questions And Answer
C++ questions And Answer
 

Recently uploaded

How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Product School
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Aggregage
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Albert Hoitingh
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
Ralf Eggert
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
sonjaschweigert1
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Ramesh Iyer
 
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
UiPathCommunity
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Nexer Digital
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 

Recently uploaded (20)

How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
 
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 

OOPS With CSharp - Jinal Desai .NET

  • 1. Articles from Jinal Desai .NET OOPS With CSharp 2012-11-10 13:11:59 Jinal Desai Some scenarios I came across while working with C# where OOPs funda deviated at some level to provide more flexibility. Suggest me some more scenarios if I miss anyone. Scenario # 1. Two interface having same method signature is derived by a class. How to implement methods in a class with same signature from different interfaces? We can use named identifiers to identify the common method we implemented based on two different interfaces. i.e. If there is two interfaces i1 and i2, both has one method with same signature (void test(int a)), then at the time of implementing these methods inside class “abc” we can use i1.test and i2.test to differentiate the implementation of methods from two different interfaces having same signature. Interface i1 { void test(int a); } interface i2 { void test(int a); } public class abc: i1, i2 { void i1.test(int a) { Console.WriteLine(“i1 implementation: “ + a); } void i2.test(int a) { Console.WriteLine(“i2 implementation: “ + a); } } Note : The only thing that can be noted here is that at the time of implementing test() methods from two different interfaces in a single class, the public modifier is not
  • 2. required. If you define public modifier for the implementation of test() method in class “abc” then it will give you error “The modifier ‘public’ is not valid for this item”. How to call those implemented methods from another class? To access methods implemented in a class “abc” into another class, we need to use respected interface rather than class “abc”. i.e. If I need to use method of interface i1 then I need to do code as follow. i1 testi1=new abc(); i1.test(10); Above code will call test() method of interface i1. In the similar manner we can call test() method of interface i2. If we need to call both the method with one instance of class “abc” then we need to cast “abc” class into an interface of which method we are intended to call as demonstrated below. abc testabc=new abc(); (testabc as i1).test(10); (testabc as i2).test(10); What if I has a method inside class “abc” with same signature prototyped in interface i1 and i2 (those interfaces we have implemented in class “abc”)? In that case we can call the method in a normal way with the instance of class “abc”. i.e. public class abc: i1, i2 { void i1.test(int a) { Console.WriteLine(“i1 implementation: “ + a); } void i2.test(int a) { Console.WriteLine(“i2 implementation: “ + a); } void test(int a) { Console.WriteLine(“Normal method inside the class: ” + a); } } //To call the method normally. abc testabc=new abc(); testabc.test(10); How we can access implemented method of i1 from inside the implemented
  • 3. method of i1, in a class “abc” or vice-versa? It’s simple. We need to cast “this” into respected interface to use it’s method. i.e. public class abc: i1, i2 { void i1.test(int a) { Console.WriteLine(“i1 implementation: “ + a); } void i2.test(int a) { Console.WriteLine(“i2 implementation: “ + a); (this as i1).test(a + 20); } } Scenario #2 Which are the things allowed inside an interface in case of C#? (In other way can automated property/event/delegate…etc allowed inside an interface while using C#.NET?) First thing first is method signature can be declared in an interface, it is the purpose of an interface. Except method signature, property declaration and event declaration are also allowed inside an interface. Property Declaration inside an Interface interface Itest { int a { get; set; } int b { get; set; } } The only thing you need to care here is that, you need to implement these properties in an implemented class. Compiler will not understand these declared properties as automated properties. In a class if we write properties like this then it should be considered as an automated properties, implementation is not required in that case. i.e. Implementation of properties defined in Itest class Test:ITest { int aVar = 0; int bVar = 0; public int a { get {
  • 4. return aVar; } set { aVar = value; } } public int b { get { return bVar; } set { bVar = value; } } } Event Declaration inside an Interface public delegate void ChangedEventHandler (object sender, EventArgs e); interface Itest { event ChangedEventHandler Changed; } Now to use this event inside the implemented class, following is an example. Class Test: Itest { public void BindEvent() { if(ChangedEventHandler!=null) Changed+=new ChangedEventHandler(Test_Changed); } void Test_Changed(object sender, EventArgs e) { //Event Fired } } This way you can confirm that all the implementer classes implemented the event to become sync with the interface design. So that all the classes can be called in a similar pattern designed by an interface. Scenario #3
  • 5. I have one interface Itest as defined below. Interface Itest { int sum(int a, int b); } I have implemented the interface into Calculator class as follow. Class Calculator:ITest { public int sum(int a, int b) { return (a+b); } } I have one more class named ScientificCalculator derived from Calculator class having same method with same signature. Class ScientificCalculator:Calculator { public int sum(int a, int b) { return base.sum(a,b); } } Can it be possible to access sum method of calculator class if I created instance of ScientificCalculator class by assigning it to interface Itest?. i.e. Itest itest = new ScientificCalculator(); Now is it possible to access sum method of Calculator class through object itest? Yes, we can access it but for that we need to cast itest object into Calculator class. ((Calculator)itest).Sum(20, 30); If you access directly sum method then it obviously refer sum method of class ScientificCalculator.