Check object type and cast object type - CSharp Custom Type

CSharp examples for Custom Type:as

Description

Check object type and cast object type

Demo Code

using System;/* www. ja v  a  2s .c o m*/
using System.Collections.Generic;
using System.Text;
public enum SportType
{
    Individual,
    Team
}
public class Athlete : Person
{
    public SportType SportType { get; set; }
    public Athlete(string firstName, string lastName, int age, SportType type) : base(firstName, lastName, age)
    {
        SportType = type;
    }
}
public enum EmployeeType
{
    Salaried,
    Hourly
}
public class Employee : Person
{
    public EmployeeType Type { get; set; }
    public Employee(string firstName, string lastName, int age, EmployeeType type) : base(firstName, lastName, age)
    {
        Type = type;
    }
}
public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Age { get; set; }
    public Person(string firstName, string lastName, int age)
    {
        FirstName = firstName;
        LastName = lastName;
        Age = age;
    }
    public override string ToString()
    {
        return $"{FirstName} {LastName}";
    }
}
public enum StudentLevel
{
    HighSchool,
    Undergrad,
    Postgrad
}
public class Student : Person
{
    public StudentLevel Level { get; set; }
    public Student(string firstName, string lastName, int age, StudentLevel level) : base(firstName, lastName, age)
    {
        Level = level;
    }
}
class Program
{
    static void Main(string[] args)
    {
        Person p = new Employee("A", "B", 23, EmployeeType.Hourly);
        describeEmployee(p);
    }
    private static void describeEmployee(Person p)
    {
        if (p is Employee e)
        {
            if (e.Type == EmployeeType.Hourly)
            {
                Console.WriteLine($"{e} is an hourly employee");
            }
            else
            {
                Console.WriteLine($"{e} is a salaried employee");
            }
        }
        else
        {
            Console.WriteLine($"{p} is not an employee");
        }
    }
    private static void describePerson(Person p)
    {
        switch (p)
        {
            case Student s when s.Level == StudentLevel.HighSchool:
                Console.WriteLine($"{s} is a high school student");
                break;
            case Student s when s.Level == StudentLevel.Undergrad:
                Console.WriteLine($"{s} is an undergraduate student");
                break;
            case Student s when s.Level == StudentLevel.Postgrad:
                Console.WriteLine($"{s} is a postgraduate student");
                break;
            case Employee e:
                Console.WriteLine($"{e} is an employee");
                break;
            case Athlete a:
                Console.WriteLine($"{a} is an athlete");
                break;
            case Person person:
                Console.WriteLine($"{p} is just a person");
                break;
            default:
                throw new ArgumentException("p is not a person");
        }
    }
}

Result


Related Tutorials