Book Image

Improving your C# Skills

By : Ovais Mehboob Ahmed Khan, John Callaway, Clayton Hunt, Rod Stephens
Book Image

Improving your C# Skills

By: Ovais Mehboob Ahmed Khan, John Callaway, Clayton Hunt, Rod Stephens

Overview of this book

This Learning Path shows you how to create high performing applications and solve programming challenges using a wide range of C# features. You’ll begin by learning how to identify the bottlenecks in writing programs, highlight common performance pitfalls, and apply strategies to detect and resolve these issues early. You'll also study the importance of micro-services architecture for building fast applications and implementing resiliency and security in .NET Core. Then, you'll study the importance of defining and testing boundaries, abstracting away third-party code, and working with different types of test double, such as spies, mocks, and fakes. In addition to describing programming trade-offs, this Learning Path will also help you build a useful toolkit of techniques, including value caching, statistical analysis, and geometric algorithms. This Learning Path includes content from the following Packt products: • C# 7 and .NET Core 2.0 High Performance by Ovais Mehboob Ahmed Khan • Practical Test-Driven Development using C# 7 by John Callaway, Clayton Hunt • The Modern C# Challenge by Rod Stephens
Table of Contents (26 chapters)
Title Page
Copyright and Credits
About Packt
What to Know Before Getting Started
Files and Directories
Advanced C# and .NET Features


The following sections describe solutions to the preceding problems. You can download the example solutions to see additional details and to experiment with the programs at

38. Directory size, LINQ style

Both this solution and Solution 30. Directory size, use the DirectoryInfo class's GetFiles method to make an array holding FileInfo objects that represent the files contained within the directory. The previous solution then used the following code to add the files' sizes:

// Add the file sizes.
long size = 0;
foreach (FileInfo fileinfo in fileinfos) size += fileinfo.Length;
return size;

This code loops through FileInfo objects and adds their file lengths to the size variable.

The new SizeLINQ extension method uses the following code to perform the same task:

// Add the file sizes.
var sizeQuery =
    from FileInfo fileinfo in fileinfos
    select fileinfo.Length;
return sizeQuery.Sum();

This version...