Skip to content

Instantly share code, notes, and snippets.

@Zolomon
Created June 19, 2013 16:02
Show Gist options
  • Select an option

  • Save Zolomon/5815516 to your computer and use it in GitHub Desktop.

Select an option

Save Zolomon/5815516 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DerivedPropertyDemo
{
class Program
{
internal class Service
{
public int ID { get; set; }
}
internal class RHService : Service
{
public string SomeOtherPropery { get; set; }
}
internal class VAService : Service
{
public string SomeOtherPropery { get; set; }
}
internal class Base
{
public List<Service> Services { get; set; }
}
internal class DerivedVA : Base
{
public List<VAService> Services { get; set; }
}
internal class DerivedRH : Base
{
public List<RHService> Services { get; set; }
}
public static void PrintWithBase(Base obj)
{
try
{
obj.Services.ForEach(x => Console.WriteLine("BaseObj: " + x.ID.ToString()));
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
static void Main(string[] args)
{
Base baseObj = new Base() {Services = new List<Service>() {new Service() {ID = 0}}};
DerivedRH rhObj = new DerivedRH() { Services = new List<RHService>() { new RHService() { ID = 1 } } };
DerivedVA vaObj = new DerivedVA() { Services = new List<VAService>() { new VAService() { ID = 2 } } };
baseObj.Services.ForEach(x => Console.WriteLine("BaseObj: " + x.ID.ToString())); // 0
rhObj.Services.ForEach(x => Console.WriteLine("RHService: " + x.ID.ToString())); // 1
vaObj.Services.ForEach(x => Console.WriteLine("VAService: " + x.ID.ToString())); // 2
PrintWithBase(baseObj);
PrintWithBase(rhObj); // null, eftersom base.Services blir 'shadowed'
PrintWithBase(vaObj); // null, eftersom base.Services blir 'shadowed'
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment