Table of Contents....5
About the Author....14
Acknowledgments....15
Introduction....16
Chapter 1: .NET....19
.NET: What Is It?....19
.NET Core: What Is It?....20
C# Language Versioning....21
.NET and C# Compilation Process....23
Compile Time and Runtime....24
Framework and Library....25
Library....25
Framework....26
Managed and Unmanaged Code....28
Chapter Summary....28
Chapter 2: Software Installation....30
About the .NET Framework....30
Installing the .NET Framework....31
Verify the .NET Framework Installation....36
Installing Visual Studio....38
Verify the Visual Studio Installation....41
Chapter Summary....42
Chapter 3: Introduction....43
Computer Program....43
Programming Languages....44
A Computer Program: A Recipe....45
Type in C#....48
The Basic Operations of a Computer....53
C# Program Application Formats....53
Format 1: Console Application....53
Format 2: .NET MAUI....55
Format 3: ASP.NET Web Applications....56
The Structure of a C# Program....56
Namespaces....62
Classes....63
Naming a Class: Class Identifiers....65
Chapter Summary....66
Chapter 4: Input and Output....68
Write to and Read from the Console....68
Change Console Display Settings....84
Chapter Summary....90
Chapter 5: Commenting Code....92
C# Single-Line Comments....94
New .NET 6 Templates....99
C# Multiple-Line Comments....104
Chapter Summary....106
Chapter 6: Data Types....108
Data Types, Variables, and Conversion....108
Data Types....108
Conversion from One Data Type to Another....111
Converting....112
Something a Little Different with Our Variables....135
C# 8 Nullable Reference Types....141
Chapter Summary....155
Chapter 7: Casting and Parsing....158
Data Types, Casting, and Parsing....158
Chapter Summary....172
Chapter 8: Arithmetic....174
Arithmetic Operations....174
Common Arithmetic Operators....176
Integer Division....177
Solution Explorer and Project Analysis....181
Other Operators....199
Plus Equals ( +=)....202
Minus Equals ( -=)....203
Multiply Equals ( *=)....203
Divide Equals ( /=)....204
Square Root....205
Chapter Summary....207
Chapter 9: Selection....209
Arithmetic Operations....209
Selection....209
Comparison Operators....210
The if Construct....217
The if-else Construct....222
The if else if Construct....224
The switch Construct....231
The switch Construct Using when....239
switch with Strings....245
switch with Strings....253
Additional Example....253
Logical Operators....261
Using the AND Operator....263
Using the OR Operator....269
Using the NOT Operator....274
Conditional Operator (Ternary Operator)....278
Nested Ternary Conditional Operator....282
Chapter Summary....287
Chapter 10: Iteration....289
Iteration and Loops....289
Introduction to Iteration....289
For Loop....290
Break Statement....304
Continue Statement....308
While Loop....312
Break Statement....321
Continue Statement....323
Do (While) Loop....327
Break Statement....335
Continue Statement....338
Chapter Summary....341
Chapter 11: Arrays....343
Arrays: A Data Structure....343
Single-Dimensional Arrays....346
Choice 1: Declaring and Creating an Array in Two Stages....348
Choice 2: Declaring and Creating an Array in One Stage....349
Referencing the Array Elements....352
foreach Loop....362
IndexOutOfBounds Exception....374
Ranges and Indices: C# 8 and Above....376
Range....382
Chapter Summary....386
Chapter 12: Methods....388
Methods: Modularization....388
Methods: Concepts of Methods and Functions....388
Some Points Regarding Methods....390
Three Types of Methods....394
Void Methods....395
Value Methods....410
Parameter Methods....420
Method Overloading....438
C# 7 Local Function....442
C# 8 Static Local Function....447
C# 10 Null Parameter Checking....450
C# 10 Null Parameter Checking Approach....454
Chapter Summary....456
Chapter 13: Classes....459
Classes and Objects in OOP....459
A Class Is a Data Structure....460
Type 1: Fields....461
Type 2: Constants....462
Type 3: Methods....463
Type 4: Properties....465
Type 5: Constructor....466
Constructor Overloading....471
Constructor....502
Another Constructor....507
Additional Example for Classes and Objects....515
CircleFormulae Class....516
RectangleFormulae Class....524
Chapter Summary....533
Chapter 14: Interfaces....534
Interfaces and Abstract Classes....534
The Interface or Abstract Class as a Manager....536
Instantiate the Abstract Class?....544
Static Members of the Abstract Class....550
Concept of an Interface....563
Implementing Multiple Interfaces....573
Concept of Default Method in an Interface....593
Concept of Static Methods and Fields in an Interface....601
Chapter Summary....603
Chapter 15: String Handling....605
String Handling and Manipulation....605
String Literals....607
Substring....611
Length....614
StartsWith()....615
Split()....617
CompareTo()....621
ToUpper() and ToLower()....625
Concat()....626
Trim()....627
Replace()....629
Contains()....631
IndexOf()....632
Insert()....634
String.Format()....637
Formatting the Items in the String....638
String Interpolation....640
String Interpolation: Spacing....643
@ Verbatim....644
What About $@ or @$?....647
Const String Interpolation....649
Chapter Summary....654
Chapter 16: File Handling....656
File Handling....656
An Overview of File Handling....657
File Class....658
Writing to a File....667
WriteAllText()....667
WriteAllLines()....669
WriteAllBytes()....670
Reading from a File....674
ReadAllText()....674
ReadAllLines()....676
ReadAllBytes()....678
Copy a File....680
Copy()....681
Delete a File....683
Delete()....684
StreamReader Class....686
Stream....686
Synchronous and Asynchronous....687
StreamReader Class Methods....688
StreamWriter Class....688
Reading from a Stream....689
Writing to a Stream....691
Async Methods and Asynchronous Programming....694
WriteLineAsync....695
FileStream....696
FileModes....696
Chapter Summary....700
Chapter 17: Exception Handling....702
Exceptions....702
What Is an Exception?....702
try....705
catch....706
finally....708
throw....708
Multiple Exceptions....713
FileNotFoundException....716
finally....719
StackTrace....722
throw....723
rethrow....725
Chapter Summary....734
Chapter 18: Serialization....736
Serialization and Deserialization....736
Deserialization....737
Attribute [NonSerialized]....737
Serializing the Object....747
Deserializing the Serialized File to a Class....752
Access Modifier [NonSerialized]....758
Serialization Using XML....760
Serialization Using JSON....771
Chapter Summary....779
Chapter 19: Structs....781
Concept of a Struct as a Structure Type....781
Difference Between Struct and Class....782
Struct with a Default Constructor Only....784
Struct with a User Constructor....787
Struct Instantiation Without the New Keyword....788
Struct Instantiation with the New Keyword....789
Creating a Constructor....791
Creating Member Properties (Get and Set Accessors)....792
Encapsulation....796
Readonly Struct....796
Creating a Readonly Struct....797
C# 8 readonly Members....801
C# 8 Nullable Reference Types....805
Chapter Summary....810
Chapter 20: Enumerations....811
Concept of Enumerations....811
Defining an Enumeration....812
Enumeration Examples....812
Enumerated Values: Use and Scope....814
Enumeration Methods....817
Using the foreach Iteration....819
Enumeration Values: GetValues()....822
Assigning Our Own Values to the Enumeration....823
Use the GetName() and GetValues() Methods....825
Sample Application Using Enumerations....826
Chapter Summary....838
Chapter 21: Delegates....839
Concept of Delegates....839
Single Delegate....843
Declare the Delegate with Its Return Type and Method Signature....843
Instantiate the Delegate and Set Its Target Method....844
Invoke the Delegate....845
Multicast Delegates....846
Instantiate the Delegate Again and Set the New Instances’ Target Method....848
Chain the Delegates....849
Invoke the Multicast Delegate....849
More Complex Example....850
Chapter Summary....860
Chapter 22: Events....861
Concept of Events....861
Publisher and Subscriber....861
Declare an Event....864
Raise an Event....865
Handle an Event....865
Add a Method to an Event Using +=....872
Refer the Event to a Second Method Using +=....873
Refer the Event to a Third Method Using +=....875
Remove a Method from an Event Using -=....877
Chapter Summary....878
Chapter 23: Generics....880
Concept of Generics....880
Generic Class, Generic Method, Generic Parameters....884
Generic Class, Generic Method, Mixed Parameter Types....890
Generic Method Only....892
Chapter Summary....898
Chapter 24: Common Routines....899
Common Programming Routines with C#....899
Linear Search....899
Create an Application That Will Implement a Linear Search....900
Binary Search (Iterative Binary Search)....906
Bubble Sort....914
Insertion Sort....920
Chapter Summary....928
Chapter 25: Programming Labs....929
C# Practice Exercises....929
Chapter 4 Labs: WriteLine()....930
Lab 1....930
Lab 2....930
Lab 3....930
Lab 4....930
Lab 1: Possible Solution with output shown in Figure 25-1....930
Lab 2: Possible Solution with output shown in Figure 25-2....931
Lab 3: Possible Solution with output shown in Figure 25-3....932
Lab 4: Possible Solution with output shown in Figure 25-4....933
Chapter 6 Labs: Data Types....934
Lab 1....934
Lab 2....934
Lab 3....934
Lab 4....934
Lab 1: Possible Solution with output shown in Figure 25-5....935
Lab 2: Possible Solution with output shown in Figure 25-6....935
Lab 3: Possible Solution with output shown in Figure 25-7....936
Lab 4: Possible Solution with output shown in Figure 25-8....938
Chapter 7 Labs: Data Conversion and Arithmetic....942
Lab 1....942
Lab 2....943
Lab 1: Possible Solution with output shown in Figure 25-9....943
Lab 2: Possible Solution with output shown in Figure 25-10....944
Chapter 8 Labs: Arithmetic....946
Lab 1....946
Lab 1: Possible Solution with output shown in Figure 25-11....947
Chapter 9 Labs: Selection....948
Lab 1....948
Lab 2....949
Lab 3....949
Lab 1: Possible Solution with output shown in Figure 25-12....949
Lab 2: Possible Solution with output shown in Figure 25-13....950
Lab 3: Possible Solution with output shown in Figure 25-14....951
Chapter 10 Labs: Iteration....953
Lab 1....953
Lab 2....953
Lab 3....954
Lab 4....954
Lab 1: Possible Solution with output shown in Figure 25-15....954
Lab 2: Possible Solution with output shown in Figure 25-16....955
Lab 3: Possible Solution with output shown in Figure 25-17....957
Lab 4: Possible Solution with output shown in Figure 25-18....958
Chapter 11 Labs: Arrays....959
Lab 1....959
Lab 2....959
Lab 3....960
Lab 1: Possible Solution with output shown in Figure 25-19....960
Lab 2: Possible Solution with output shown in Figure 25-20....962
Lab 3: Possible Solution with output shown in Figure 25-21....963
Chapter 12 Labs: Methods....965
Lab 1....965
Lab 2....965
Lab 1: Possible Solution with output shown in Figure 25-22....966
Lab 2: Possible Solution with output shown in Figure 25-23....969
Chapter 13 Labs: Classes....972
Lab 1....972
Lab 2....973
Lab 1: Possible Solution with output shown in Figure 25-24....974
Lab 2: Possible Solution with output shown in Figure 25-25....978
Chapter 14 Labs: Interfaces....980
Lab 1....980
Lab 1: Possible Solution with output shown in Figure 25-26....981
Chapter 15 Labs: String Handling....983
Lab 1....983
Lab 2....983
Lab 1: Possible Solution with output shown in Figure 25-27....984
Lab 2: Possible Solution with output shown in Figure 25-28....985
Chapter 16 Labs: File Handling....988
Lab 1....988
Lab 2....988
Lab 1: Possible Solution with output shown in Figure 25-29....988
Lab 2: Possible Solution with output shown in Figure 25-30....990
Chapter 17 Labs: Exceptions....992
Lab 1....992
Lab 2....992
Lab 1: Possible Solution with output shown in Figure 25-31....993
Lab 2: Possible Solution with output shown in Figure 25-32....994
Chapter 18 Labs: Serialization of a Class....995
Lab 1....995
Lab 2....995
Lab 1: Possible Solution with output shown in Figure 25-33....996
Lab 2: Possible Solution with output shown in Figure 25-34....999
Chapter 19 Labs: Structs....1002
Lab 1....1002
Lab 2....1003
Lab 1: Possible Solution with output shown in Figure 25-35....1003
Lab 2: Possible Solution with output shown in Figure 25-36....1006
Chapter 20 Labs: Enumerations....1008
Lab 1....1008
Lab 2....1009
Lab 1: Possible Solution with output shown in Figure 25-37....1010
Lab 2: Possible Solution with output shown in Figure 25-38....1012
Chapter 21 Labs: Delegates....1017
Lab 1....1017
Lab 2....1018
Lab 1: Possible Solution with output shown in Figure 25-39....1019
Lab 2: Possible Solution with output shown in Figure 25-40....1021
Chapter 22 Labs: Events....1023
Lab 1....1023
Lab 2....1025
Lab 1: Possible Solution with output shown in Figure 25-41....1026
Lab 2: Possible Solution with output shown in Figure 25-42....1028
Chapter 23 Labs: Generics....1032
Lab 1....1032
Lab 2....1033
Lab 1: Possible Solution with output shown in Figure 25-43....1034
Lab 2: Possible Solution with output shown in Figure 25-44....1035
Chapter Summary....1037
Untitled....929
Chapter 26: C# 11....1039
C# New Features....1039
Raw String Literals....1040
New Lines in String Interpolations....1048
List Patterns....1050
Auto Default Struct....1056
Warning Wave 7....1061
Chapter Summary....1063
Index....1065
So, you want to learn C# and Visual Studio 2022, but are a bit intimidated? Don’t be. Programming is within your grasp! Programmers at any level have to fully understand, and more importantly, be able to code the core constructs. It is impossible to use complex programming concepts such as classes before understanding what methods and variables and their data types are. Once there is a foundation built on the basics, then all other topics can fall in line.
While it is a forgone conclusion that languages change with the introduction of new features, the core concepts do not. Even large enterprises do not always update to the latest versions of languages and frameworks; their "backbone" applications have been developed to work, regardless. More than ever, enterprises need developers who can master and apply the core programming concepts and then be "up-skilled" with newer language levels and features as they integrate into the company.
This book builds from the ground up.You will begin with an introduction to programming, learning the foundational concepts needed to become a C# programmer. You will then put to practice a wide range of programming concepts, including data types, selection, iteration, arrays, methods, classes and objects, serialization, file handling, and string handling. You will learn enough to develop applications that emulate commercial application code. Once you’ve got the foundational concepts, get ready to dive into common programming routines, including linear search, binary search, bubble sort and insertion sort, and use C# to code them. Code example annotations supplement the learning and are designed to enhance learning while also explaining why the code does what it does.
Beginners, those refreshing their C# skills, or those moving from another programming language. No skills or previous knowledge is required. Readers will need to download Visual Studio 2022 Community Edition as this is what the book code has been based on, but they could use other Integrated Development Environments.