Book Image

Beginning C# 7 Hands-On - The Core Language

By : Tom Owsiak
Book Image

Beginning C# 7 Hands-On - The Core Language

By: Tom Owsiak

Overview of this book

Beginning C# 7 Hands-On - The Core Language teaches you core C# language and syntax in a working Visual Studio environment. This book covers everything from core language through to more advanced features such as object-oriented programming techniques. This book is for C# 7 beginners who need a practical reference to core C# language features. You'll also gain a view of C# 7 through web programming with web forms, so you'll learn HTML, basic CSS, and how to use a variety of controls, such as buttons and drop-down lists. You'll start with the fundamentals of C# and Visual Studio, including defining variables, interacting with users, and understanding data types, data conversions, and constants. You'll move on to checking conditions using if/else blocks, and see how to use loops to do things such as repeat blocks of code. After covering various operators to evaluate and assign control structures, you'll see how to use arrays to store collections of data. By the time you’ve finished the book, you’ll know how to program the vital elements of the core C# language. These are the building blocks that you can then combine to build complex C# programs.
Table of Contents (60 chapters)
Free Chapter
1
Why C# and How to Download and Install the Visual Studio Community Edition
6
String Interpolation and Updating Visual Studio
12
Reacting to a Single Condition with If/Else Blocks
14
Repeating Blocks of Code with While Loops
15
Repeating Blocks of Code with For Loops
16
Iterating Over Collections with foreach Loops
17
Examining Multiple Variable Values with Switch Blocks
20
Operators That Evaluate and Assign in Place
21
Checking Two Conditions with the Logical AND Operator
22
Checking Two Conditions with the Logical OR Operator
28
Creating More Flexible Methods with the params Keyword
30
Combining the ref and out Keywords to Write Flexible Functions
33
Writing Easier Code with the Var and Dynamic Keywords
34
Creating a Class with a Constructor and a Function
41
Using Custom Types as Return Types
44
Using Interfaces to Express Common Behaviors
49
Overloading Operators to Perform Custom Operations
50
Using Enumerations to Represent Named Constants

Setting up the Book class

Let's create a project. Make a file, go to Solution Explorer, right-click on the name of the website, select Add, and click on Class. Name the class Book and click on OK. When the Visual Studio message comes up, click on Yes.

The section of the Book class where we'll begin our work looks as shown in the following screenshot:

Figure 6.14.1: Relevant starting section of the Book class

As our first step to making our Book object, enter the following under the open curly brace below public class Book:

public string Title { get; set; }

Remember, public means accessible anywhere. The get and set within curly braces is what we call an automatic property, which means that to set its values, you can set the value of the property directly inside the constructor; you don't need a backing field explicitly defined.

Alright, let's set a couple...