Archive for c#


08
Apr
2011

Multiple inheritance in c sharp

Hi there!

all of us .net programmers know that there isn’t a built in way to create multiple inheritance in c#,
well – i needed such. i know some will argue and say this pattern is a no no, and i should have used other methods to create what i wanted,
but for me – it make perfect sense to do so.
am sharing with you the simple way i did so, if it is useful for one of you, i’m glad !

?View Code CSHARP
public class Multiple  where TType1 : new()
where TType2 : new()
{
static TType1 base1;
static TType2 base2;
 
public Multiple()
{
base1 = new TType1();
base2 = new TType2();
}
 
public static implicit operator TType1(Multiple c)
{return base1;}
public static implicit operator TType2(Multiple c)
{return base2;}
}
 
class Foo{}
class Bar{}
class FooBar : Multiple
{}
 
//then use as
 
FooBar foobar=new FooBar();
((Foo)foobar).FooFunciton();
((Bar)foobar).BarFunction();
foobar.FooBarFunction();

attached is a visual studio 2010 sample code: MinheritCSharp.zip

Cheers,

Ady

Comments
Posted in ASP.NET, c#, ,

28
Mar
2011

linq distinct – create a distinct collection using linq (c#)

Hi there,

well its been a long while since i wrote anything on this blog, alot have changed in this long period.

wanted to share a neat way to use linq in c# to get a distinct list of elements by some key.
lets say you have a collection of categories, where the unique identifier called uid, and you want to make that list distinct,

var practiceAreas = from teamMember in AllTeamMembers
from teamMemberRelation in teamMember.TeamToTeamPracticeAreaRecords
select teamMemberRelation.TeamPracticeArea;
// DISTINCT ….
temp = practiceAreas.GroupBy(p => p.EntityId).Select(g => g.First()).ToList();
var categoryCollection = from item in AllItems
select item.Categories;
now make it distinct
var distinctCategoryCollection = categoryCollection .GroupBy(p => p.uid).Select(g => g.First())
neat ha ?
Comments
Posted in ASP.NET, c#, linq