| 
  • If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • Get control of your email attachments. Connect all your Gmail accounts and in less than 2 minutes, Dokkio will automatically organize your file attachments. You can also connect Dokkio to Drive, Dropbox, and Slack. Sign up for free.

View
 

Lecture - Using Delegates in C Sharp

Page history last edited by Dr. Ron Eaglin 4 years, 3 months ago

Using Delegates in C#

 

 

Prerequisites

 

Knowledge of C#

 

Summary

 

Students typically struggle with the concepts of delegates in C#, this video demonstrates how delegates are used to allow methods to be passed to methods (or used in expressions)

 

Video 

 

 

Reference Materials

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace Delegate_Examples

{

    class Program

    {

        static void Main(string[] args)

        {

            List<SampleObject> list = new List<SampleObject>();

 

            list.Add(new SampleObject("Name 1", false));

            list.Add(new SampleObject("Name 2", true));

 

            Console.Write(list.First<SampleObject>().Name);

            Console.ReadLine();

 

            Console.Write(list.First(delegate (SampleObject so) { return so.nameIsValid() == false; }).Name);

            Console.ReadLine();

 

            Console.Write(list.First<SampleObject>(so => so.nameIsValid() == false).Name);

            Console.ReadLine();

        }

    }

 

    class SampleObject

    {

        public SampleObject(String name)

        {

            Name = name;

        }

 

        public SampleObject(String name, Boolean valid)

        {

            Name = name;

            Valid = valid;

        }

 

 

        public string Name { get; set; }

        public bool Valid { get; set; }

 

        public bool nameIsValid()

        {

            // Some logic will be here

            return Valid;

        }

    }

}

  

 

 

 

 

Additional Information

 

 

COP 4834 Lectures Page

Comments (0)

You don't have permission to comment on this page.