CSharp examples for Custom Type:Polymorphism
Assign any object to type Object variable
using System;/*from w ww. j a va 2 s . co m*/ using System.Collections.Generic; using System.Text; public class Person { private string _firstName; public string FirstName { get { return _firstName; } } private string _lastName; public string LastName { get { return _lastName; } } public int Age { get; set; } public Person(string firstName, string lastName, int age) { _firstName = firstName; _lastName = lastName; Age = age; } } class Program { static void Main(string[] args) { object obj = new Person("A", "B", 32); doSomething(obj); List<int> list = new List<int>(); doSomething(list); } private static void doSomething(object o) { Person p = o as Person; if(p == null) { throw new ArgumentException("Argument is not a person"); } Console.WriteLine(p.FirstName + " " + p.LastName); } }