concept sake in category .net

This is an excerpt from Manning's book C# in Depth.
Now the properties don’t have any code (or visible variables!) associated with them, and you’re building the hardcoded list in a very different way. With no name and price variables to access, you’re forced to use the properties everywhere in the class, improving consistency. You now have a private parameterless constructor for the sake of the new property-based initialization. (This constructor is called for each item before the properties are set.)
Listing 12.3. A simple implementation of IQueryable that logs method calls
class FakeQuery<T> : IQueryable<T> { public Expression Expression { get; private set; } #1 public IQueryProvider Provider { get; private set; } public Type ElementType { get; private set; } internal FakeQuery(IQueryProvider provider, Expression expression) { Expression = expression; Provider = provider; ElementType = typeof(T); } internal FakeQuery() : this(new FakeQueryProvider(), null) { Expression = Expression.Constant(this); #2 } public IEnumerator<T> GetEnumerator() { Logger.Log(this, Expression); return Enumerable.Empty<T>().GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() #3 { Logger.Log(this, Expression); return Enumerable.Empty<T>().GetEnumerator(); } public override string ToString() { return "FakeQuery"; #4 } } #1 – 1 Declares simple automatic properties #2 – 2 Uses this query as initial expression #3 – 3 Returns empty result sequence #4 – 4 Overrides ToString for the sake of logging !@%STYLE%@! {"css":"{\"css\": \"font-weight: bold;\"}","target":"[]"} !@%STYLE%@!
The optional parameter values are simple to deal with, thanks to the null coalescing operator
. This example uses separate variables for the sake of printed formatting, but in real code you’d probably use the same expressions directly in the calls to the StreamWriter constructor and the WriteLine method.
It’s usually worth trying to avoid having side effects in arguments, but there are cases where it can make the code clearer. A more realistic rule is to try to avoid side effects that might interfere with each other. For the sake of demonstrating execution order, we’ll break both of these rules. Please don’t treat this as a recommendation that you do the same thing.
. This example uses separate variables for the sake of printed formatting, but in real code you’d probably use the same expressions directly in the calls to the StreamWriter constructor and the WriteLine method.