Is

The 'is' operator checks if an boject is of a certain type

Example

        public bool stringTest( string test)
        {
            if(test is string)
            {
                return true;
            }
            return false;
        }

You then can allso cast the value into a new variable. So if we have a teacher and a student and we want to have a single method to print the fullname ( instead off to refactor the code )

Example

    class Student : IPerson
    {
        public string FirstName { get; set; }
        public string Lastname { get; set; }

        public Student(string firstname, string lastname)
        {
            FirstName = firstname;
            Lastname = lastname;
        }

        public string FullName()
        {
            return FirstName + " " + Lastname;
        }
    }

    class Teacher : IPerson
    {
        public string FullName { get; private set; }

        public Teacher(string fullname)
        {
            FullName = fullname;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            var person = new Student("test", "Student");
            Console.WriteLine(GetFullName(person));
        }

        private static string GetFullName(IPerson person)
        {
            if(person is Student foundStudent)
            {
                return foundStudent.FullName();
            }
            if(person is Teacher foundTeacher)
            {
                return foundTeacher.FullName;
            }
            return string.Empty;
        }
    }

{{opmerking.Naam}}:

{{opmerking.OpmerkingText}}

            

Saving your comment....

Naam is verplicht!
Email is verplicht!
Opmerking is verplicht!