Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace Test
- {
- class Person { }
- class Student : Person { }
- class MainClass
- {
- public static void Main(string[] args)
- {
- Student student = new Student();
- Person studentAsPerson = student;
- Person justPerson = new Person();
- if (student is Student)
- {
- Console.WriteLine("This instance is subclass/class of Student");
- }
- if (studentAsPerson is Student)
- {
- // This will set 's' to null if studentAsPerson is not subclass/class of Student!
- var s = studentAsPerson as Student;
- // This will throw error if studentAsPerson is not subclass/class of Student!
- var st = (Student)studentAsPerson;
- Console.WriteLine("This instance is subclass/class Student");
- }
- if (student is Person)
- {
- Console.WriteLine("This instance is subckass/class of Person");
- }
- if (studentAsPerson is Person)
- {
- Console.WriteLine("This instance is subclass/class of Person");
- }
- if (studentAsPerson.GetType() == typeof(Student))
- {
- Console.WriteLine("This object is instance of class Student!");
- }
- if (justPerson is Student) //false
- {
- Console.WriteLine("Hell no!");
- }
- if ((justPerson as Student) == null) // person is not subclass of student!
- {
- Console.WriteLine("Cannot convert person to student!");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement