31 July 2013

Covariance .Net Framework 4.0

Covariance .Net Framework 4.0

Covariance preserve assignment compatibility between Parent and Child relationship during dynamic polymorphism

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Covariance
{

    class Program
    {
        static void Main(string[] args)
        {

            List objAthelete = new List();
            objAthelete.Add(new Athelete
            {
                SportName = "100 meter",
                SportsType = "Running"
            });

            IEnumerable Persons = objAthelete;

            Athelete objAth = new Athelete();
            //objAth.PrintAll(Persons);
        }
    }


    public class Person
    {
        public string Name { get; set; }
        public string City { get; set; }
    }

    public class Athelete : Person
    {

        public string SportName { get;set; }
        public string SportsType { get;set; }

        public void PrintAll(IEnumerable objects)
        {
            foreach (Athelete o in objects)
            {
                Console.WriteLine("Sports Name:" + o.SportName + "Sports Type:" + o.SportsType);
            }
        }
    }
}

The following error is thrown when the application is run in .Net framework 3.5, but it support in 4.0 framework.





No comments:

Post a Comment

Comments Welcome

Implementing OAuth validation in a Web API

 I mplementing OAuth validation in a Web API Implementing OAuth validation in a Web API using C# typically involves several key steps to sec...