C# Tips and Traps 2

jasonroberts

Jason Roberts

Welcome to this review of the Pluralsight course C# Tips and Traps 2 by Jason Roberts.

With over 15 years industry experience, Jason is a Microsoft .NET MVP who has has written multiple books including Clean C#, and C# Tips. He is also an open source contributor and the creator of FeatureToggle.

This course forms part of the following learning paths:

C# Tips & Traps 2 by Jason Roberts

This is volume 2 of Jason’s compendium of Tips and Tricks. It is shorter than the first volume with only four modules. The same productivity and note taking tips for C# Tips & Traps also apply to this course.

Part 1

This module builds on what you learned two of the previous courses in this learning path.

If you have watched C# Events, Delegates and Lambdas, Dan Wahlin’s coverage of Delegates will come back into mind as Jason teaches Multicast Delegates, and Compatibility and Equality of Delegates.

If you have watched Scott Allen’s C# Generics course you will have seen some of the ways in which you can constrain Generics. In Part 1 of this C# Tips & Traps 2, you will see all of the ways in which you can constrain them: by base class, by interface, by value type and by parameterless constructor.

Jason also shows how to use these constraints in combination with each other.

Part 2

Jason shows that when structs contain only value members, performance of equality checks is much faster. This is because reflection is used to determine equality for reference types. The MSDN documentation for ValueType.Equals Method explains more. Overriding the equals method is a technique for dramatically improving performance.

Then Jason shows that conditional operators such as && or || are short circuited (or lazy evaluated) but the & and | operators are not short circuited.

Enumerable.Range is introduced. This is also featured in the Practical LINQ course, and is useful for auto-generating a sequence of Integer values.

If you have an enumerable sequence of strings, such as name, and a sequence of integers, such as ages, you might want to merge them together into one sequence. Jason demonstrates that using a lambda can protect you in cases where the length of the two sequences are not the same.

You might also want to perform set operations on your sequence, such as Concat, Union, Intersect and Except. This tip is covered both here and also in the Practical LINQ course.

A LINQ tip that is not covered in the Practical LINQ course, is how to do custom aggregation. Jason uses an overload of Aggregate specifying a seed value.

I had never seen interface members explicitly implemented before, always implementing them implicitly. It was good to learn this, but it seems like a strange thing to do and I don’t plan to start using this technique.

Task.Delay can be a better alternative than Thread.Sleep, and Jason demonstrates how this is used.

The last tip in this module is how to use the SortedSetSortedList and SortedDictionary classes.

For a more in-depth look at Sets in C#, see module 9 of Simon Robinson’s C# Collections Fundamentals course.

Part III

Customizing The Display of An Object in The Debugger Window with the DebuggerTypeProxy attribute

Recreating Hidden Files Without Exceptions

Working with Zip Files – System.IO.Compression and System.IO.Compression.FileSystem

Compressing and Decompressing in-memory Streams

Working with URIs

Part IV

Using Generic Methods in Non-Generic Classes

Deferring Creation of Resource Intensive Objects Until Use https://msdn.microsoft.com/en-us/library/dd997286.aspx

Automatically Skip Code When Debugging with the [DebuggerStepThrough] attribute

Subscribing to File Changed Events with System.IO.FileSystemWatcher

Nullable Value Types and Ambient Properties

 

Recommended speed: 1.6x

3 thoughts on “C# Tips and Traps 2

  1. Pingback: C# End to End Learning Path | Zombie Code Kill

  2. Pingback: C# Generics | Zombie Code Kill

  3. Pingback: C# Tips and Traps | Zombie Code Kill

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s