Book Image

C# 7 and .NET: Designing Modern Cross-platform Applications

By : Mark J. Price, Ovais Mehboob Ahmed Khan
Book Image

C# 7 and .NET: Designing Modern Cross-platform Applications

By: Mark J. Price, Ovais Mehboob Ahmed Khan

Overview of this book

C# is a widely used programming language, thanks to its easy learning curve, versatility, and support for modern paradigms. The language is used to create desktop apps, background services, web apps, and mobile apps. .NET Core is open source and compatible with Mac OS and Linux. There is no limit to what you can achieve with C# and .NET Core. This Learning Path begins with the basics of C# and object-oriented programming (OOP) and explores features of C#, such as tuples, pattern matching, and out variables. You will understand.NET Standard 2.0 class libraries and ASP.NET Core 2.0, and create professional websites, services, and applications. You will become familiar with mobile app development using Xamarin.Forms and learn to develop high-performing applications by writing optimized code with various profiling techniques. By the end of C# 7 and .NET: Designing Modern Cross-platform Applications, you will have all the knowledge required to build modern, cross-platform apps using C# and .NET. This Learning Path includes content from the following Packt products: • C# 7.1 and .NET Core 2.0 - Modern Cross-Platform Development - Third Edition by Mark J. Price • C# 7 and .NET Core 2.0 High Performance by Ovais Mehboob Ahmed Khan
Table of Contents (25 chapters)
Title Page
Copyright
About Packt
Contributors
Preface
16
Designing Guidelines for .NET Core Application Performance
Index

Iteration statements


Iteration statements repeat a block either while a condition is true or for each item in a group. The choice of which statement to use is based on a combination of ease of understanding to solve the logic problem and personal preference.

Use either Visual Studio 2017 or Visual Studio Code to add a new console application project named IterationStatements.

In Visual Studio 2017, you can set the solution's start up project to be the current selection so that the current project runs when you press Ctrl + F5.

The while statement

The while statement evaluates a Boolean expression and continues to loop while it is true.

Type the following code inside the Main method:

int x = 0; 
while (x < 10) 
{ 
   WriteLine(x); 
   x++; 
} 

Run the console application and view the output:

0123456789

The do statement

The do statement is like while, except the Boolean expression is checked at the bottom of the block instead of the top, which means that it always executes at least once.

Type the following code at the end of the Main method and run it:

string password = string.Empty; 
do 
{ 
   Write("Enter your password: "); 
   password = ReadLine(); 
} while (password != "secret"); 
WriteLine("Correct!"); 

You will be prompted to enter your password repeatedly until you enter it correctly, as shown in the following output:

Enter your password: password
Enter your password: 12345678
Enter your password: ninja
Enter your password: asdfghjkl
Enter your password: secret
Correct!

As an optional exercise, add statements so that the user can only make ten attempts before an error message is displayed.

The for statement

The for statement is like while, except that it is more succinct. It combines an initializer statement that executes once at the start of the loop, a Boolean expression to check whether the loop should continue, and an incrementer that executes at the bottom of the loop. The for statement is commonly used with an integer counter, as shown in the following code:

for (int y = 1; y <= 10; y++)
{
   WriteLine(y);
}

Run the console application and view the output, which should be the numbers 1 to 10.

The foreach statement

The foreach statement is a bit different from the other three iteration statements. It is used to perform a block of statements on each item in a sequence, for example, an array or collection. Each item is read-only, and if the sequence is modified during iteration, for example, by adding or removing an item, then an exception will be thrown.

Type the following code inside the Main method, which creates an array of string variables and then outputs the length of each of them:

string[] names = { "Adam", "Barry", "Charlie" };
foreach (string name in names)
{
   WriteLine($"{name} has {name.Length} characters.");
}

Run the console application and view the output:

Adam has 4 characters.
Barry has 5 characters.
Charlie has 7 characters.

Technically, the foreach statement will work on any type that implements an interface called IEnumerable. An interface is a contract and you will learn more about them in Chapter 4,Implementing Interfaces and Inheriting Classes.

The compiler turns the foreach statement in the preceding code into something like this:

IEnumerator e = names.GetEnumerator();
while (e.MoveNext())
{ 
   string name = (string)e.Current; // Current is read-only!
   WriteLine($"{name} has {name.Length} characters.");
}

Note

Due to the use of an iterator, the variable declared in a foreach statement cannot be used to modify the value of the current item.