Your SlideShare is downloading. ×
OOPS With CSharp - Jinal Desai .NET
OOPS With CSharp - Jinal Desai .NET
OOPS With CSharp - Jinal Desai .NET
OOPS With CSharp - Jinal Desai .NET
OOPS With CSharp - Jinal Desai .NET
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

OOPS With CSharp - Jinal Desai .NET

378

Published on

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.

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.

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
378
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
16
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Articles from Jinal Desai .NETOOPS With CSharp2012-11-10 13:11:59 Jinal DesaiSome scenarios I came across while working with C# where OOPs funda deviatedat some level to provide more flexibility. Suggest me some more scenarios if I missanyone.Scenario # 1.Two interface having same method signature is derived by a class.How to implement methods in a class with same signature from differentinterfaces?We can use named identifiers to identify the common method we implementedbased 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 fromtwo 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 inclass “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 touse 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 calltest() method of interface i2. If we need to call both the method with one instance ofclass “abc” then we need to cast “abc” class into an interface of which method weare 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 ininterface 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 #2Which are the things allowed inside an interface in case of C#? (In other waycan automated property/event/delegate…etc allowed inside an interfacewhile using C#.NET?)First thing first is method signature can be declared in an interface, it is the purposeof an interface. Except method signature, property declaration and event declarationare also allowed inside an interface.Property Declaration inside an Interfaceinterface Itest{ int a { get; set; } int b { get; set; }}The only thing you need to care here is that, you need to implement these propertiesin an implemented class. Compiler will not understand these declared properties asautomated properties. In a class if we write properties like this then it should beconsidered as an automated properties, implementation is not required in that case.i.e. Implementation of properties defined in Itestclass 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 Interfacepublic 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 tobecome sync with the interface design. So that all the classes can be called in asimilar 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 classhaving 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 ofScientificCalculator 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 classScientificCalculator.

×