So you understand lexical and dynamic scoping in C# do you? I thought I did. I didn’t. It took me some trial and error, and searching things out on the internet and technical documentation to bed-in my understanding.
Test yourself here. There is some sample code below, 10 questions. All you have to do is determine what is printed out to the console for each of the 10 questions. Simple stuff! My bet is you will score 10/10 or 0/10 in my little quiz.
Here’s a hint: execution of a compiled version of the C# code will produce a few lines of output containing Db-1000001, Db-1000002, Dc-1, Dc-2, E1, and E2 (questions 8 and 9). The other questions are as simple, honest.
using System; using System.Collections.Generic; using System.Threading.Tasks; namespace ConsoleApp1743 { class Program { static void Main(String[] args) { Func<int, String> funcA = (a) => "A" + a.ToString(); var tasks = new List<Task>(); for (int i = 1; i <= 2; i++) { //Question 01: Add 1 mark is you correctly determined the console output for i=1 and i=2 for this task tasks.Add(new Task(() => Console.WriteLine(i))); //Question 02: Add 1 mark is you correctly determined the console output for i=1 and i=2 for this task tasks.Add(new Task(() => Console.WriteLine(i.ToString()))); //Question 03: Add 1 mark is you correctly determined the console output for i=1 and i=2 for this task tasks.Add(new Task(() => Console.WriteLine(0 - i))); //Question 04: Add 1 mark is you correctly determined the console output for i=1 and i=2 for this task Action action = () => Console.WriteLine(1000 + i); Task task = new Task(action); tasks.Add(task); //Question 05: Add 1 mark is you correctly determined the console output for i=1 and i=2 for this task tasks.Add(new Task(() => Console.WriteLine(funcA(i)))); //Question 06: Add 1 mark is you correctly determined the console output for i=1 and i=2 for this task Func<int, String> funcB = delegate (int b) { return "B" + b.ToString(); }; tasks.Add(new Task(() => Console.WriteLine(funcB(i)))); //Question 07: Add 1 mark is you correctly determined the console output for i=1 and i=2 for this task Func<int, String> funcC = delegate (int c) { return "C" + c.ToString(); }; tasks.Add(new Task(() => Console.WriteLine(funcC(i)))); //Question 08: Add 1 mark is you correctly determined the console output for i=1 and i=2 for this task Func<int, String, String> funcD = delegate (int d, String s) { return s + d.ToString(); }; int d = i; tasks.Add(new Task(() => Console.WriteLine(funcD(d, "Da-")))); d = d + 1000000; tasks.Add(new Task(() => Console.WriteLine(funcD(d, "Db-")))); //console contains Db-000001 and Db-000002 int dc = i; tasks.Add(new Task(() => Console.WriteLine(funcD(dc, "Dc-")))); //console contains Dc-1 and Dc-2 //Question 09: Add 1 mark is you correctly determined the console output for i=1 and i=2 for this task String e = "E" + i.ToString(); tasks.Add(new Task(() => Console.WriteLine(e))); //Question 10: Add 1 mark is you correctly determined the console output for i=1 and i=2 for this task tasks.Add(new Task(() => Console.WriteLine("F" + i.ToString()))); } Parallel.ForEach(tasks, t => { t.Start(); t.Wait(); }); } } }
Oh, one last thing, there are 0 points allocated for you recognizing that task completion as invoked via. Parallel.ForEach results in tasks completing in their own time and the order of lines in the console output is non-deterministic.
Good luck!
The actual console output from one execution of this program is shown below. Scroll down, and then scroll some more. If you scored 10/10 first time, you are a better developer than I am!
C:\VS\Projects\ConsoleApp1743\ConsoleApp1743\bin\Debug> C:\VS\Projects\ConsoleApp1743\ConsoleApp1743\bin\Debug> C:\VS\Projects\ConsoleApp1743\ConsoleApp1743\bin\Debug> C:\VS\Projects\ConsoleApp1743\ConsoleApp1743\bin\Debug>ConsoleApp1743.exe 3 C3 3 3 C3 Da-1000002 Db-1000002 Dc-2 E2 3 F3 Dc-1 E1 F3 A3 1003 A3 B3 -3 B3 Da-1000001 Db-1000001 -3 1003 C:\VS\Projects\ConsoleApp1743\ConsoleApp1743\bin\Debug> C:\VS\Projects\ConsoleApp1743\ConsoleApp1743\bin\Debug>
The console output above looks wrong, but is right. Be honest with yourself, you didn’t get it right first time did you?
Oh, give yourself a bonus point if you recognized that variable d in question 08 above has already been declared in scope, and that in order to get the program above to compile you will have to rename d and its references to, for example, _d 🙂
— Published by Mike, 19:17:22 31 January 2018 (CET)
By Month: November 2022, October 2022, August 2022, February 2021, January 2021, December 2020, November 2020, March 2019, September 2018, June 2018, May 2018, April 2018
Apple, C#, Databases, Faircom, General IT Rant, German, Informatics, LINQ, MongoDB, Oracle, Perl, PostgreSQL, SQL, SQL Server, Unit Testing, XML/XSLT
Leave a Reply