0% found this document useful (0 votes)
17 views13 pages

Delegates

wasds
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views13 pages

Delegates

wasds
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Delegates

using System;
using System.Collections.Generic;

namespace Delegates
{
public delegate bool IsClearedDelegate(Student stu);
public class Student
{
public int ID { get; set; }
public string Name { get; set; }
public int Marks { get; set; }

public static void ClearedAssessment(List<Student> sList, IsClearedDelegate


isEligibleToClear)
{
foreach (Student s in sList)
{
if (isEligibleToClear(s))
{
Console.WriteLine(s.Name + " - Cleared Assessment");
}
}
}
}
internal class Program
{
static void Main(string[] args)
{
List<Student> students = new List<Student>();
students.Add(new Student() { ID = 111, Name = "John", Marks = 68 });
students.Add(new Student() { ID = 222, Name = "Scott", Marks = 85 });
students.Add(new Student() { ID = 333, Name = "Roger", Marks = 81 });
students.Add(new Student() { ID = 444, Name = "Shyam", Marks = 35 });
IsClearedDelegate delObj = new IsClearedDelegate(StudentPassCriteria);
//delObj(s);

Student.ClearedAssessment(students, delObj);
}

public static bool StudentPassCriteria(Student s)


{
if(s.Marks>=80)
{
return true;
}
else
{
return false;
}
}
}
}

has context menu


Delegates

A delegate is a type safe function pointer, that holds reference to a function.

 The return type and Signature of the delegate must match with the return type and
signature of the function that delegate points to.
 Syntax of the delegate looks very similar to a method with a delegate keyword.

Multicast Delegates

A multicast delegate is a delegate that has references to more than one function.

When we invoke a multicast delegate, all the functions that delegate is pointing to are invoked.

A multicast delegate invokes the methods in the same order in which they are referenced.

sing System;
namespace MulticastDelegates
{
public delegate void MySampleDelegate();
internal class Program
{
static void Main(string[] args)
{
MySampleDelegate delObj = new MySampleDelegate(Method1);
delObj += Method3;
delObj += Method2;
delObj();
}
public static void Method1()
{
Console.WriteLine("Method1 invoked..!!");
}
public static void Method2()
{
Console.WriteLine("Method2 invoked..!!");
}
public static void Method3()
{
Console.WriteLine("Method3 invoked..!!");
}
}
}

sing System;

namespace MulticastDelegates
{
public delegate void MySampleDelegate();
internal class Program
{
static void Main(string[] args)
{
MySampleDelegate delObj = new MySampleDelegate(Method1);
delObj += Method3;
delObj += Method2;
delObj();
}
public static void Method1()
{
Console.WriteLine("Method1 invoked..!!");
}
public static void Method2()
{
Console.WriteLine("Method2 invoked..!!");
}
public static void Method3()
{
Console.WriteLine("Method3 invoked..!!");
}
}
}

Reflection is the ability of inspecting an assembly metadata at runtime. It is used to find all types
in an assembly and dynamically invoke methods in the assembly.

using System;
using System.IO;

namespace FileHandling
{
internal class Program
{
static void Main(string[] args)
{

// Working with Drives


DriveInfo[] dInfo = DriveInfo.GetDrives();
Console.WriteLine("Total Partitions");
Console.WriteLine("------------------------");
foreach(DriveInfo d in dInfo)
{
Console.WriteLine(d.Name);
}
Console.WriteLine();
Console.WriteLine("Enter the partition : ");
string driveName = Console.ReadLine();
DriveInfo driveInfo = new DriveInfo(driveName);
Console.WriteLine();
Console.WriteLine("Drive Name : {0}",driveInfo.Name);
Console.WriteLine("Total Space : {0}", driveInfo.TotalSize);
Console.WriteLine("Free Space : {0}", driveInfo.TotalFreeSpace);
Console.WriteLine("Drive Format : {0}",driveInfo.DriveFormat);
Console.WriteLine("Volume Label : {0}",driveInfo.VolumeLabel);
Console.WriteLine("Drive Type : {0}",driveInfo.DriveType);

//Working with Directories


Console.WriteLine("----------------------------------------");
DirectoryInfo di = new DirectoryInfo(@"D:\Hands-On");
Console.WriteLine("------Directory Information----");
Console.WriteLine();
Console.WriteLine("FullName : {0}",di.FullName);
Console.WriteLine("Root : {0}",di.Root);
Console.WriteLine("Creation Time : {0}", di.CreationTime);
Console.WriteLine("Parent : {0}",di.Parent);

//Creating Directory and sub-directory


Console.WriteLine("Create Directory & Sub-directories");
DirectoryInfo dirInfo = new DirectoryInfo(@"D:\Demos");
dirInfo.Create();
dirInfo.CreateSubdirectory("Temp");
dirInfo.CreateSubdirectory("Temp\\Sample");
Console.WriteLine("Directory & Sub-Directory created successfully..!!");

Console.WriteLine("--------------------------------------------------------");
Console.WriteLine("Delete Directory");
DirectoryInfo deleteDirInfo = new DirectoryInfo(@"D:\Demos");
Console.WriteLine("Name : {0}", deleteDirInfo.FullName);
Console.WriteLine("Are you sure to delete..!!");
string str = Console.ReadLine();
if(str == "y" || str=="Y")
{
Directory.Delete(@"D:\Demos", true);
Console.WriteLine("Directory deleted..!!");
}
}
}
}

2 Surprised reactions.2 using System; using System.IO; namespac... by T V S KoushikT V S


Koushik11:47 PM
using System;
using System.IO;

namespace FileHandling
{
internal class Program
{
static void Main(string[] args)
{
//Reading & Writing to a file using Stream Reader & StreamWriter Class

//Writing data to a file using StreamWriter


string path = @"D:\Demos\Sample.txt";
using(StreamWriter writer = new StreamWriter(path))
{
writer.WriteLine("This is a sample program");
writer.WriteLine("On File Handling using streamWriter");
}
Console.WriteLine("File created and text written to it successfully..!!");

//Reading data from a file using StreamReader


using(StreamReader reader = new StreamReader(path))
{
string content = reader.ReadToEnd();
Console.WriteLine("----File content----");
Console.WriteLine(content);
}

}
}
}
using System;
using System.IO;

namespace FileHandling
{
internal class Program
{
static void Main(string[] args)
{
//Reading & Writing to a file using Stream Reader & StreamWriter Class

//Writing data to a file using StreamWriter


string path = @"D:\Demos\Sample.txt";
using(StreamWriter writer = new StreamWriter(path))
{
writer.WriteLine("This is a sample program");
writer.WriteLine("On File Handling using streamWriter");
}
Console.WriteLine("File created and text written to it successfully..!!");

//Reading data from a file using StreamReader


using(StreamReader reader = new StreamReader(path))
{
string content = reader.ReadToEnd();
Console.WriteLine("----File content----");
Console.WriteLine(content);
}
}
}
}

using System;
using System.IO;

namespace FileHandling
{
internal class Program
{
static void Main(string[] args)
{
//Reading & Writing to a file using Stream Reader & StreamWriter Class

//Writing data to a file using StreamWriter


string path = @"D:\Demos\Sample.txt";
using(StreamWriter writer = new StreamWriter(path))
{
writer.WriteLine("This is a sample program");
writer.WriteLine("On File Handling using streamWriter");
}
Console.WriteLine("File created and text written to it successfully..!!");

//Reading data from a file using StreamReader


using(StreamReader reader = new StreamReader(path))
{
string content = reader.ReadToEnd();
Console.WriteLine("----File content----");
Console.WriteLine(content);
}
}
}
}

using System; using System.IO; namespac... by T V S KoushikT V S KoushikYesterday 11:52


PM

using System;
using System.IO;

namespace FileHandling
{
internal class Program
{
static void Main(string[] args)
{
//Reading & Writing to a file using File class

string path = @"D:\Demos\Sample123.txt";


string fileContent = "This is sample content to be stored in a file named Sample123.txt
using File class";
File.WriteAllText(path,fileContent);
Console.WriteLine("File created and text written to it successfully..!!");

//Reading data from a file using File class


string contentFromFile = File.ReadAllText(path);
Console.WriteLine("---File Content---");
Console.WriteLine(contentFromFile);

}
}
}

using System; using System.IO; namespac... by T V S KoushikT V S KoushikYesterday 11:58


PM
using System;
using System.IO;

namespace FileHandling
{
internal class Program
{
static void Main(string[] args)
{
//Append data to the existing file using StreamWriter & File classes

//Append data to a file using StreamWriter


string path = @"D:\Demos\Sample.txt";
using(StreamWriter writer = new StreamWriter(path,true))
{
writer.WriteLine("Appending this content at 12:25 PM on 02-Jul-2024");
}
Console.WriteLine("File appended successfully..!!");

string path2 = @"D:\Demos\Sample123.txt";


string content = " This is the content being appended using File class to sample123.txt";
File.AppendAllText(path2, content);
Console.WriteLine("Text appended..!!");

}
}
}

Today using System; using System.IO; namespac... by T V S KoushikT V S Koushik12:08 AM

using System;
using System.IO;
namespace FileHandling
{
internal class Program
{
static void Main(string[] args)
{
//StringWriter and StringReader -> these classes are used for reading form and writing to
strings
//as if they were streams. these classes are used to perform In-memory text manipulation

//In-Memory text generation

using(StringWriter writer = new StringWriter())


{
writer.WriteLine("Hello, World..!!");
writer.WriteLine("This is a text written to a stringWriter.");
string result = writer.ToString();
Console.WriteLine(result);
}

//StringReader class is used for In-memory text reading

string content = "MySample text being written to test\n StringReader class";


using(StringReader reader = new StringReader(content))
{
string line;
while((line = reader.ReadLine()) != null)
{
Console.WriteLine(line);
}
}

}
}
}
has context menu

has context menu

You might also like