Friday, April 20, 2007 12:02 PM
bart
C# 3.0 Quiz - Core LINQ concepts
All C# quizzes on my blog so far were covering C# 2.0 language features. Today, we'll start with a first quiz on C# 3.0, which is rather a knowledge check than a quiz in the classic sense of the word. This quiz focuses on the core concepts in C# 3.0 that drive LINQ.
Consider the code fragment below:
Defect piece of LINQ code -
Copy Code 1 //
2 // Put additional namespace imports here
3 //
4
5 class Program
6 {
7 static void Main()
8 {
9 Source<Customer> src = null;
10 var res = from s in src
11 orderby s.Name, s.Age descending
12 where s.Name == "Bart"
13 select s.Age;
14 }
15 }
16
17 class Customer
18 {
19 public string Name { get; set; }
20 public int Age { get; set; }
21 }
22
23 abstract class Source<T>
24 {
25 //
26 // Put code here
27 //
28 }
This piece of code doesn't compile with the following messages:
Error 1 The type arguments for method 'System.Linq.Queryable.OrderBy<TSource,TKey>(System.Linq.IQueryable<TSource>, System.Linq.Expressions.Expression<System.Linq.Func<TSource,TKey>>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
The goal of this quiz is to provide a minimal abstract definition of the Source<T> class in order to make the code compile. We won't execute the application though, we're just focusing on the compilation (and the result of the compilation).
A few rules:
- You're only allowed to put abstract method signatures in regions marked by comments.
- You shouldn't implement any interface (after all, the previous rule doesn't allow touching line 23).
- Any method you add to Source<T> should return an object of the generic type Source`1.
Additional questions:
- Two different (categories of) solutions for implementation of Source<T> exist, explain.
- Will these two solutions mentioned in question 1 produce different IL code for Program::Mail? If so, explain why and point out the differences.
- How many different minimal solutions exist (renaming a parameter doesn't count for a different solution of course)?
Have fun!
Del.icio.us |
Digg It |
Technorati |
Blinklist |
Furl |
reddit |
DotNetKicks
Filed under: C# 3.0, LINQ