Untitled

mail@pastecode.io avatar
unknown
csharp
3 years ago
1.8 kB
1
Indexable
Never
using System;
using System.Collections.Generic;

namespace Learn
{
    //Delegate declaration, this delegate
    //points to a function which accepts integer & returns boolean
    public delegate bool IsPassedExam(int totalMarks);

    public class Student
    {
        public readonly int totalMarks;
        public string Name { get; set; }

        public Student(string name, int totalMarks)
        {
            this.Name = name;
            this.totalMarks = totalMarks;
        }

        //Below method expects the condition to determine
        //whether a student pass or fails from the user calling it.
        public void CheckIfPassed(IsPassedExam isPassedExam)
        {
            //isPassedExam calls the user provided function.
            if (isPassedExam(this.totalMarks))
            {
                Console.WriteLine($"Student {this.Name} passed in exam");
            }
            else
            {
                Console.WriteLine($"Student {this.Name} failed in exam");
            }
        }



    }

    class Learn
    {
        //user provided function to determin
        //whether a student pass or fail.
        public static bool ConditionForPassing(int marks)
        {
            return marks > 50;
        }

        public static void Main(string[] args)
        {
            //sample list of students
            List<Student> students = new List<Student>
                {
                new Student("Hari",90),
                new Student("Tom",98),
                new Student("John",46),
                new Student("James",49)
                };

            IsPassedExam isPassedExam = new IsPassedExam(ConditionForPassing);

            foreach (Student student in students)
            {
                student.CheckIfPassed(isPassedExam);
            }

        }

    }
}