13,090,541 members (47,460 online)
Rate this:
See more: , +
Hello All,

I want to implement two interfaces to one class and use only one definition for function like Microsoft Collection< T > interface Add function . Is it possible

With regards ,
Jophy
Posted 29-Jun-12 22:06pm
Updated 30-Jun-12 0:51am
v2

Rate this:

## Solution 1

// Declare the English units interface:
interface IEnglishDimensions
{
float Length();
float Width();
}

// Declare the metric units interface:
interface IMetricDimensions
{
float Length();
float Width();
}

// Declare the Box class that implements the two interfaces:
// IEnglishDimensions and IMetricDimensions:
class Box : IEnglishDimensions, IMetricDimensions
{
float lengthInches;
float widthInches;

public Box(float length, float width)
{
lengthInches = length;
widthInches = width;
}

// Explicitly implement the members of IEnglishDimensions:
float IEnglishDimensions.Length()
{
return lengthInches;
}

float IEnglishDimensions.Width()
{
return widthInches;
}

// Explicitly implement the members of IMetricDimensions:
float IMetricDimensions.Length()
{
return lengthInches * 2.54f;
}

float IMetricDimensions.Width()
{
return widthInches * 2.54f;
}

static void Main()
{
// Declare a class instance box1:
Box box1 = new Box(30.0f, 20.0f);

// Declare an instance of the English units interface:
IEnglishDimensions eDimensions = (IEnglishDimensions)box1;

// Declare an instance of the metric units interface:
IMetricDimensions mDimensions = (IMetricDimensions)box1;

// Print dimensions in English units:
System.Console.WriteLine("Length(in): {0}", eDimensions.Length());
System.Console.WriteLine("Width (in): {0}", eDimensions.Width());

// Print dimensions in metric units:
System.Console.WriteLine("Length(cm): {0}", mDimensions.Length());
System.Console.WriteLine("Width (cm): {0}", mDimensions.Width());
}
}
/* Output:
Length(in): 30
Width (in): 20
Length(cm): 76.2
Width (cm): 50.8
*/
Rahul Rajat Singh 30-Jun-12 4:36am

+5 for working code and good answer. I am not sure what the OP is really asking for but still good effort. even i am trying to answer based on my understanding. the only difference between my answer and yours is that i moved some checking inside the class rather than from the caller end.
Rate this:

## Solution 2

1. I am not sure I understood why you might want to do this.
2. If you really NEED to do something like this then perhaps you need to rethink design.
3. code from Member 8070578 seems correct from logic standpoint but you will have to see if it helps you. (nevertheless +5 to that answer for giving some working code.)
4. I have a small example in which you can implement all interface functions with same name in a class and then expose a single function outside. inside this single function you will have to see which actual implementation needs to be called. (although this is working code, i would not recommend this at all. as if we are doing something like this their is some serious design flaw)

namespace C1L1
{
interface one
{
}

interface Two
{
}

public enum TYPE
{
SOME_TYPE,
SOME_OTHER_TYPE
}

class dummy : one, Two
{
TYPE type_;
public dummy(TYPE type)
{
type_ = type;
}
#region Two Members

{
return a + b;
}

#endregion

#region one Members

{
return a + b;
}

#endregion

public int Add(int a, int b)
{
int result;
if (type_ == TYPE.SOME_TYPE)
{
}
else
{
}

return result;
}
}
class Program
{
static void Main(string[] args)
{
dummy d1 = new dummy(TYPE.SOME_TYPE);

dummy d2 = new dummy(TYPE.SOME_OTHER_TYPE);

}
}
}
v2
jophyjob 30-Jun-12 6:39am

I like to do is that different type of parameters in function
like this
interface IBase
{
}

interface IBase2< T >
{
}
and use like

class AA<t> : IBase2<t> , IBase
{
public void add(T o) { } // Is it posible to work only by this
// implimentation
}
Microsoft "Collection < T > " class use this way in Add(T item)
Rate this:

## Solution 3

Hello All,
I get it .... the code of ICollection<T> See is
public class Collection<T> : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable
{

really the code have the implementation of two Add functions

And

If you see with DeCompiler, you can see this implementation under List<t> like this,
{
List<T>.VerifyValueType(item);
return this.Count - 1;
}

since this scope is private for IList.Add So you are not able to see in object browser.
Thank you All

Top Experts
Last 24hrsThis month
 Graeme_Grant 130 RickZeeland 120 OriginalGriff 105 ppolymorphe 75 Wonde Tadesse 60
 OriginalGriff 3,462 Graeme_Grant 1,914 ProgramFOX 1,707 Jochen Arndt 1,645 ppolymorphe 1,552