Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
C# 7 and .NET Core 2.0 High Performance

You're reading from  C# 7 and .NET Core 2.0 High Performance

Product type Book
Published in Apr 2018
Publisher
ISBN-13 9781788470049
Pages 300 pages
Edition 1st Edition
Languages
Author (1):
Ovais Mehboob Ahmed Khan Ovais Mehboob Ahmed Khan
Profile icon Ovais Mehboob Ahmed Khan

Table of Contents (11) Chapters

Preface 1. What's New in .NET Core 2 and C# 7? 2. Understanding .NET Core Internals and Measuring Performance 3. Multithreading and Asynchronous Programming in .NET Core 4. Data Structures and Writing Optimized Code in C# 5. Designing Guidelines for .NET Core Application Performance 6. Memory Management Techniques in .NET Core 7. Securing and Implementing Resilience in .NET Core Applications 8. Microservices Architecture 9. Monitoring Application Performance Using Tools 10. Other Books You May Enjoy

Data Structures and Writing Optimized Code in C#

Data structures are a particular way of storing data in software engineering. They play a vital role in storing data in a computer so that it can be accessed and modified efficiently, and they provide different storing mechanisms for storing different types of data. There are many types of data structure, and each one is designed to store a definite type of data. In this chapter, we will cover data structures in detail and learn which data structures should be used for particular scenarios in order to improve the performance of the system as regards data storage and retrieval. We will also learn how we can write optimized code in C# and what primary factors can affect performance, which is sometimes overlooked by developers when coding programs. We will learn some best practices that can be used to optimize code that is performance...

What are data structures?

A data structure is a way of storing and unifying data in such a way that operations on that data can be performed in an efficient manner. The data can be stored in several ways. For example, we can have a Person object that contains a few properties, such as PersonID and PersonName, where PersonID is of the integer type and PersonName is of the string type. This Person object stores the data in memory, and can be further used to save that record in the database. Another example is an array called Countries of the string type that contains a list of countries. We can use the Countries array to retrieve a country name and use it in a program. Therefore, any type of object that stores data is called a data structure. All primitive types, such as integers, strings, chars, and Booleans, are different types of data structure, whereas other collection types...

Understanding the use of Big O notation to measure the performance and complexity of an algorithm

Big O notation is used to define the complexity and performance of an algorithm with respect to time or space consumed during execution. It is an essential technique to express the performance of an algorithm and determine the worst-case complexity of the program.

To understand it in detail, let's go through some code examples and use Big O notation to calculate their performance.

If we calculate the complexity of the following program, the Big O notation will be equal to O(1):

static int SumNumbers(int a, int b) 
{ 
  return a + b; 
} 

This is because, however the parameter is specified, it is just adding and returning it.

Let's consider another program that loops through the list. The Big O notation will be determined as O(N):

static bool FindItem(List<string> items...

Choosing the right data structure for performance optimization

A data structure is a precise way of organizing data in a computer program. If data is not efficiently stored in the right data structure, it may lead to some performance issues that impact the overall experience of the application.

In this section, we will learn the advantages and disadvantages of the different collection types available in .NET Core and which ones are the better types for particular scenarios:

  • Arrays and lists
  • Stacks and queues
  • LinkedLists (single, double, and circular)
  • Dictionaries, hashtables, and hashsets
  • Generic lists

Arrays

An array is a collection that holds similar types of elements. Arrays of both value types and reference types can...

Best practices in writing optimized code in C#

There are many factors that negatively impact the performance of a .NET Core application. Sometimes these are minor things that were not considered earlier at the time of writing the code, and are not addressed by the accepted best practices. As a result, to solve these problems, programmers often resort to ad hoc solutions. However, when bad practices are combined together, they produce performance issues. It is always better to know the best practices that help developers write cleaner code and make the application performant.

In this section, we will learn, the following topics:

  • Boxing and unboxing overhead
  • String concatenation
  • Exceptions handling
  • for versus foreach
  • Delegates

Boxing and unboxing overhead

...

Summary

In this chapter, we have learned the core concepts about data structures, the types of data structures, as well as their advantages and disadvantages, followed by the best possible scenarios in which each can be used. We also learned about the Big O notation, which is one of the core topics to consider when writing code and helps developers to identify code performance. Finally, we looked into some best practices and covered topics such as boxing and unboxing, string concatenation, exception handling, for and foreach loops, and delegates.

In the next chapter, we will learn some guidelines and best practices that could be helpful when designing .NET Core applications.

lock icon The rest of the chapter is locked
You have been reading a chapter from
C# 7 and .NET Core 2.0 High Performance
Published in: Apr 2018 Publisher: ISBN-13: 9781788470049
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}