Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learn C# Programming

You're reading from   Learn C# Programming A guide to building a solid foundation in C# language for writing efficient programs

Arrow left icon
Product type Paperback
Published in Apr 2020
Publisher Packt
ISBN-13 9781789805864
Length 636 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Authors (4):
Arrow left icon
Raffaele Rialdi Raffaele Rialdi
Author Profile Icon Raffaele Rialdi
Raffaele Rialdi
Ankit Sharma Ankit Sharma
Author Profile Icon Ankit Sharma
Ankit Sharma
Prakash Tripathi Prakash Tripathi
Author Profile Icon Prakash Tripathi
Prakash Tripathi
Marius Bancila Marius Bancila
Author Profile Icon Marius Bancila
Marius Bancila
Arrow right icon
View More author details
Toc

Table of Contents (20) Chapters Close

Preface 1. Chapter 1: Starting with the Building Blocks of C# 2. Chapter 2: Data Types and Operators FREE CHAPTER 3. Chapter 3: Control Statements and Exceptions 4. Chapter 4: Understanding the Various User-Defined Types 5. Chapter 5: Object-Oriented Programming in C# 6. Chapter 6: Generics 7. Chapter 7: Collections 8. Chapter 8: Advanced Topics 9. Chapter 9: Resource Management 10. Chapter 10: Lambdas, LINQ, and Functional Programming 11. Chapter 11: Reflection and Dynamic Programming 12. Chapter 12: Multithreading and Asynchronous Programming 13. Chapter 13: Files, Streams, and Serialization 14. Chapter 14: Error Handling 15. Chapter 15: New Features of C# 8 16. Chapter 16: C# in Action with .NET Core 3 17. Chapter 17: Unit Testing 18. Assessments 19. Other Books You May Enjoy

Creating threads in .NET

Creating a raw thread is something that mostly makes sense only when you have a long-running operation that depends on the CPU alone. As an example, let's say we want to compute prime numbers, without really caring about the possible optimizations:

public class Primes : IEnumerable<long>
{
	public Primes(long Max = long.MaxValue)
	{
		this.Max = Max;
	}
	public long Max { get; private set; }
	IEnumerator IEnumerable.GetEnumerator() => ((IEnumerable<long>)this).GetEnumerator();
	public IEnumerator<long> GetEnumerator()
	{
		yield return 1;
		bool bFlag;
		long start = 2;
		while (start < Max)
		{
			bFlag = false;
			var number = start;
			for (int i = 2; i < number; i++)
			{
				if (number % i == 0)
				{
					bFlag = true;
					break;
				}
			}
			if (!bFlag)
			{
				yield return number;
			}
			start++;
		}
	}
}

The Primes class implements IEnumerable<long> so that we can easily enumerate the prime numbers in...

lock icon The rest of the chapter is locked
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 $19.99/month. Cancel anytime
Banner background image