×
Login Register an account
Top Submissions Explore Upgoat Search Random Subverse Random Post Colorize! Site Rules Donate
2

starting new job soon, coming back up to speed in C#. thought I would run through a producer/consumer pattern using delegates to oil the rusty parts of my brain :-)

submitted by AugustineOfHippo2 to programming 3.0 yearsMay 10, 2022 16:25:33 ago (+3/-1)     (programming)

using System;

public delegate void del_call(int prntVal);//defining the delegate

public class ProduceRandom {
del_call prDel;//local instance of delegate
public ProduceRandom(del_call pp) {
prDel = pp;//constructor to populate the local delegate
}
public void makeRandom() {
Random rnd = new Random();
for(int i = 0; i<10; i++ ) {
prDel( rnd.Next(0,10) );//generate 10 random numbers (from 0 to 10)
}
}
}//end class ProduceRandom
public class ConsumeRandom {
public void printRandom(int rVal) {
Console.WriteLine("Random = " + rVal); //delegate target, just print to console
}
}//end class ConsumeRandom
public class xyz{
public static void Main(string[] args) {
ConsumeRandom cr1 = new ConsumeRandom();//caveat - must instantiate consumer prior to producer
ProduceRandom pr1 = new ProduceRandom(cr1.printRandom);
pr1.makeRandom();
}//end Main
}//end class


5 comments block

You surely live in a liberal hell hole

Off yourself