
Beginning C# 6 Programming with Visual Studio 2015
Description
Alles über E-Books | Antworten auf Fragen rund um E-Books, Kopierschutz und Dateiformate finden Sie in unserem Info- & Hilfebereich.
More details
Other editions
Additional editions

Persons
Content
PART I: THE OOP LANGUAGE
CHAPTER 1: INTRODUCING C# 3
What Is the .NET Framework? 4
What's in the .NET Framework? 4
Writing Applications Using the .NET Framework 5
What Is C#? 8
Applications You Can Write with C# 9
C# in this Book 10
Visual Studio 2015 10
Visual Studio Express 2015 Products 10
Solutions 11
CHAPTER 2: WRITING A C# PROGRAM 13
The Visual Studio 2015 Development Environment 14
Console Applications 17
The Solution Explorer 20
The Properties Window 21
The Error List Window 22
Desktop Applications 22
CHAPTER 3: VARIABLES AND EXPRESSIONS 29
Basic C# Syntax 30
Basic C# Console Application Structure 33
Variables 34
Simple Types 34
Variable Naming 39
Literal Values 39
Expressions 42
Mathematical Operators 42
Assignment Operators 47
Operator Precedence 48
Namespaces 49
CHAPTER 4: FLOW CONTROL 53
Boolean Logic 54
Boolean Bitwise and Assignment Operators 56
Operator Precedence Updated 58
Branching 59
The Ternary Operator 59
The if Statement 59
The switch Statement 63
Looping 66
do Loops 66
while Loops 69
for Loops 71
Interrupting Loops 72
Infinite Loops 73
CHAPTER 5: MORE ABOUT VARIABLES 77
Type Conversion 78
Implicit Conversions 78
Explicit Conversions 80
Explicit Conversions Using the Convert Commands 83
Complex Variable Types 85
Enumerations 85
Structs 89
Arrays 92
String Manipulation 99
CHAPTER 6: FUNCTIONS 107
Defining and Using Functions 108
Return Values 110
Parameters 112
Variable Scope 119
Variable Scope in Other Structures 122
Parameters and Return Values versus Global Data 123
The Main() Function 125
Struct Functions 127
Overloading Functions 128
Using Delegates 130
CHAPTER 7: DEBUGGING AND ERROR HANDLING 135
Debugging in Visual Studio 136
Debugging in Nonbreak (Normal) Mode 136
Debugging in Break Mode 144
Error Handling 153
try...catch...finally 153
Listing and Configuring Exceptions 160
CHAPTER 8: INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING 163
What Is Object-Oriented Programming? 164
What Is an Object? 165
Everything's an Object 168
The Life Cycle of an Object 168
Static and Instance Class Members 169
OOP Techniques 170
Interfaces 171
Inheritance 172
Polymorphism 175
Relationships between Objects 177
Operator Overloading 179
Events 180
Reference Types versus Value Types 180
OOP in Desktop Applications 180
CHAPTER 9: DEFINING CLASSES 187
Class Definitions in C# 188
Interface Definitions 190
System.Object 193
Constructors and Destructors 195
Constructor Execution Sequence 196
OOP Tools in Visual Studio 200
The Class View Window 200
The Object Browser 202
Adding Classes 203
Class Diagrams 204
Class Library Projects 206
Interfaces versus Abstract Classes 209
Struct Types 212
Shallow Copying versus Deep Copying 214
CHAPTER 10: DEFINING CLASS MEMBERS 217
Member Definitions 218
Defining Fields 218
Defining Methods 219
Defining Properties 220
Refactoring Members 225
Automatic Properties 226
Additional Class Member Topics 227
Hiding Base Class Methods 227
Calling Overridden or Hidden Base Class Methods 229
Using Nested Type Defi nitions 230
Interface Implementation 232
Implementing Interfaces in Classes 233
Partial Class Definitions 235
Partial Method Definitions 237
Example Application 238
Planning the Application 238
Writing the Class Library 239
A Client Application for the Class Library 246
The Call Hierarchy Window 248
CHAPTER 11: COLLECTIONS, COMPARISONS, AND CONVERSIONS 251
Collections 252
Using Collections 253
Defining Collections 258
Indexers 259
Adding a Cards Collection to CardLib 262
Keyed Collections and IDictionary 264
Iterators 266
Iterators and Collections 270
Deep Copying 271
Adding Deep Copying to CardLib 273
Comparisons 275
Type Comparisons 275
Value Comparisons 279
Conversions 295
Overloading Conversion Operators 295
The as Operator 297
CHAPTER 12: GENERICS 301
What Are Generics? 302
Using Generics 303
Nullable Types 303
The System.Collections.Generic Namespace 311
Defining Generic Types 321
Defining Generic Classes 322
Defining Generic Interfaces 332
Defining Generic Methods 333
Defining Generic Delegates 334
Variance 335
Covariance 336
Contravariance 336
CHAPTER 13: ADDITIONAL C# TECHNIQUES 341
The : : Operator and the Global Namespace Qualifi er 342
Custom Exceptions 343
Adding Custom Exceptions to CardLib 343
Events 345
What Is an Event? 345
Handling Events 347
Defining Events 350
Expanding and Using CardLib 357
Attributes 365
Reading Attributes 366
Creating Attributes 367
Initializers 368
Object Initializers 368
Collection Initializers 371
Type Inference 374
Anonymous Types 376
Dynamic Lookup 380
The dynamic Type 381
Advanced Method Parameters 384
Optional Parameters 385
Named Parameters 386
Lambda Expressions 391
Anonymous Methods Recap 391
Lambda Expressions for Anonymous Methods 392
Lambda Expression Parameters 396
Lambda Expression Statement Bodies 396
Lambda Expressions as Delegates and Expression Trees 398
Lambda Expressions and Collections 399
PART II: WINDOWS PROGRAMMING
CHAPTER 14: BASIC DESKTOP PROGRAMMING 407
XAML 408
Separation of Concerns 409
XAML in Action 409
The Playground 411
WPF Controls 412
Properties 413
Events 417
Control Layout 422
Stack Order 423
Alignment, Margins, Padding, and Dimensions 423
Border 424
Canvas 424
DockPanel 426
StackPanel 428
WrapPanel 429
Grid 430
The Game Client 433
The About Window 433
The Options Window 439
Data Binding 448
Starting a Game with the ListBox Control 453
CHAPTER 15: ADVANCED DESKTOP PROGRAMMING 461
The Main Window 462
The Menu Control 462
Routed Commands with Menus 462
Creating and Styling Controls 466
Styles 467
Templates 467
Value Converters 472
Triggers 473
Animations 475
WPF User Controls 478
Implementing Dependency Properties 478
Putting It All Together 489
Refactoring the Domain Model 489
The View Models 494
Completing the Game 502
PART III: CLOUD PROGRAMMING
CHAPTER 16: BASIC CLOUD PROGRAMMING 515
The Cloud, Cloud Computing, and the Cloud Optimized Stack 516
Cloud Patterns and Best Practices 519
Using Microsoft Azure C# Libraries to Create a Storage Container 520
Creating an ASP.NET 4.6 Web Site That Uses the Storage Container 530
CHAPTER 17: ADVANCED CLOUD PROGRAMING AND DEPLOYMENT 539
Creating an ASP.NET Web API 540
Deploying and Consuming an ASP.NET Web API on Microsoft Azure 544
Scaling an ASP.NET Web API on Microsoft Azure 551
PART IV: DATA ACCESS
CHAPTER 18: FILES 561
File Classes for Input and Output 562
The File and Directory Classes 563
The FileInfo Class 564
The DirectoryInfo Class 566
Path Names and Relative Paths 566
Streams 567
Classes for Using Streams 567
The FileStream Object 568
The StreamWriter Object 575
The StreamReader Object 577
Asynchronous File Access 580
Reading and Writing Compressed Files 580
Monitoring the File System 584
CHAPTER 19: XML AND JSON 593
XML Basics 594
JSON Basics 594
XML Schemas 595
XML Document Object Model 597
The XmlDocument Class 598
The XmlElement Class 598
Changing the Values of Nodes 603
Converting XML to JSON 609
Searching XML with XPath 611
CHAPTER 20: LINQ 617
LINQ to XML 618
LINQ to XML Functional Constructors 618
Working with XML Fragments 621
LINQ Providers 624
LINQ Query Syntax 624
Declaring a Variable for Results Using the var Keyword 626
Specifying the Data Source: from Clause 627
Specify Condition: where Clause 627
Selecting Items: select Clause 627
Finishing Up: Using the foreach Loop 628
Deferred Query Execution 628
LINQ Method Syntax 628
LINQ Extension Methods 629
Query Syntax versus Method Syntax 629
Lambda Expressions 630
Ordering Query Results 632
Understanding the orderby Clause 633
Querying a Large Data Set 634
Using Aggregate Operators 636
Using the Select Distinct Query 640
Ordering by Multiple Levels 642
Using Group Queries 644
Using Joins 646
CHAPTER 21: DATABASES 651
Using Databases 651
Installing SQL Server Express 652
Entity Framework 652
A Code First Database 653
But Where Is My Database? 660
Navigating Database Relationships 661
Handling Migrations 668
Creating and Querying XML from an
Existing Database 669
PART V: ADDITIONAL TECHNIQUES
CHAPTER 22: WINDOWS COMMUNICATION FOUNDATION 677
What Is WCF? 678
WCF Concepts 679
WCF Communication Protocols 679
Addresses, Endpoints, and Bindings 680
Contracts 682
Message Patterns 683
Behaviors 683
Hosting 683
WCF Programming 684
The WCF Test Client 690
Defining WCF Service Contracts 693
Self-Hosted WCF Services 700
CHAPTER 23: UNIVERSAL APPS 709
Getting Started 709
Universal Apps 710
App Concepts and Design 711
Screen Orientation 711
Menus and Toolbars 711
Tiles and Badges 712
App Lifetime 712
Lock Screen Apps 712
App Development 712
Adaptive Displays 713
Sandboxed Apps 721
Navigation between Pages 725
The CommandBar Control 728
Managing State 729
Common Elements of Windows Store Apps 732
The Windows Store 733
Packaging an App 733
Creating the Package 734
APPENDIX: EXERCISE SOLUTIONS 737
INDEX 781
Introduction
C# is a relatively new language that was unveiled to the world when Microsoft announced the first version of its .NET Framework in July 2000. Since then its popularity has rocketed, and it has arguably become the language of choice for desktop, web, and cloud developers who use the .NET Framework. Part of the appeal of C# comes from its clear syntax, which derives from C/C++ but simplifies some things that have previously discouraged some programmers. Despite this simplification, C# has retained the power of C++, and there is now no reason not to move into C#. The language is not difficult and it's a great one to learn elementary programming techniques with. This ease of learning, combined with the capabilities of the .NET Framework, make C# an excellent way to start your programming career.
The latest release of C#, C# 6, which is included with version 4.6 of the .NET Framework, builds on the existing successes and adds even more attractive features. The latest release of Visual Studio (Visual Studio 2015) and the Visual Studio Express/Community 2015 line of development tools also bring many tweaks and improvements to make your life easier and to dramatically increase your productivity.
This book is intended to teach you about all aspects of C# programming, including the language itself, desktop and cloud programming, making use of data sources, and some new and advanced techniques. You'll also learn about the capabilities of Visual Studio 2015 and all the ways that this product can aid your application development.
The book is written in a friendly, mentor-style fashion, with each chapter building on previous ones, and every effort is made to ease you into advanced techniques painlessly. At no point will technical terms appear from nowhere to discourage you from continuing; every concept is introduced and discussed as required. Technical jargon is kept to a minimum; but where it is necessary, it, too, is properly defined and laid out in context.
The authors of this book are all experts in their field and are all enthusiastic in their passion for both the C# language and the .NET Framework. Nowhere will you find a group of people better qualified to take you under their collective wing and nurture your understanding of C# from first principles to advanced techniques. Along with the fundamental knowledge it provides, this book is packed full of helpful hints, tips, exercises, and full-fledged example code (available for download at ) that you will find yourself returning to repeatedly as your career progresses.p2p.wrox.com
We pass this knowledge on without begrudging it and hope that you will be able to use it to become the best programmer you can be. Good luck, and all the best!
Who This Book Is For
This book is for everyone who wants to learn how to program in C# using the .NET Framework. It is for absolute beginners who want to give programming a try by learning a clean, modern, elegant programming language. But it is also for people familiar with other programming languages who want to explore the .NET platform, as well as for existing .NET developers who want to give Microsoft's .NET flagship language a try.
What This Book Covers
The early chapters cover the language itself, assuming no prior programming experience. If you have programmed in other languages before, much of the material in these chapters will be familiar. Many aspects of C# syntax are shared with other languages, and many structures are common to practically all programming languages (such as looping and branching structures). However, even if you are an experienced programmer, you will benefit from looking through these chapters to learn the specifics of how these techniques apply to C#.
If you are new to programming, you should start from the beginning, where you will learn basic programming concepts and become acquainted with both C# and the .NET platform that underpins it. If you are new to the .NET Framework but know how to program, you should read Chapter 1 and then skim through the next few chapters before continuing with the application of the C# language. If you know how to program but haven't encountered an object-oriented programming language before, you should read the chapters from Chapter 8 onward.
Alternatively, if you already know the C# language, you might want to concentrate on the chapters dealing with the most recent .NET Framework and C# language developments, specifically the chapters on collections, generics, and C# language enhancements (Chapters 11 to 13), or skip the first section of the book completely and start with Chapter 14.
The chapters in this book have been written with a dual purpose in mind: They can be read sequentially to provide a complete tutorial in the C# language, and they can be dipped into as required reference material.
In addition to the core material, starting with Chapter 3 each chapter also includes a selection of exercises at the end, which you can work through to ensure that you have understood the material. The exercises range from simple multiple choice or true/false questions to more complex exercises that require you to modify or build applications. The answers to all the exercises are provided in Appendix A. You can also find these exercises as part of the wrox.com code downloads for this book at www.wrox.com/go/beginningvisualc#2015programming.
This book also gives plenty of love and attention to coincide with the release of C# 6 and .NET 4.6. Every chapter received an overhaul, with less relevant material removed, and new material added. All of the code has been tested against the latest version of the development tools used, and all of the screenshots have been retaken in Windows 8.1/10 to provide the most current windows and dialog boxes.
New highlights of this edition include the following:
- Additional and improved code examples for you to try out
- Coverage of everything that's new in C# 6 and .NET 4.6, including how to create Universal Windows Apps
- Examples of programming cloud applications and using Azure SDK to create and access cloud resources
How This Book Is Structured
This book is divided into six sections:
- Introduction - Purpose and general outline of the book's contents
- The C# Language - Covers all aspects of the C# language, from the fundamentals to object-oriented techniques
- Windows Programming - How to write and deploy desktop applications with the Windows Presentation Foundation library (WPF)
- Cloud Programming - Cloud application development and deployment, including the creation and consumption of a Web API
- Data Access - How to use data in your applications, including data stored in files on your hard disk, data stored in XML format, and data in databases
- Additional Techniques - An examination of some extra ways to use C# and the .NET Framework, including Windows Communication Foundation (WCF) and Universal Windows Applications
The following sections describe the chapters in the five major parts of this book.
The C# Language (Chapters 1-13)
Chapter 1 introduces you to C# and how it fits into the .NET landscape. You'll learn the fundamentals of programming in this environment and how Visual Studio 2015 (VS) fits in.
Chapter 2 starts you off with writing C# applications. You'll look at the syntax of C# and put the language to use with sample command-line and Windows applications. These examples demonstrate just how quick and easy it can be to get up and running, and along the way you'll be introduced to the Visual Studio development environment and the basic windows and tools that you'll be using throughout the book.
Next you'll learn more about the basics of the C# language. You'll learn what variables are and how to manipulate them in Chapter 3. You'll enhance the structure of your applications with flow control (looping and branching) in Chapter 4, and you'll see some more advanced variable types such as arrays in Chapter 5. In Chapter 6 you'll start to encapsulate your code in the form of functions, which makes it much easier to perform repetitive operations and makes your code much more readable.
By the beginning of Chapter 7 you'll have a handle on the fundamentals of the C# language, and you will focus on debugging your applications. This involves looking at outputting trace information as your applications are executed, and at how Visual Studio can be used to trap errors and lead you to solutions for them with its powerful debugging environment.
From Chapter 8 onward you'll learn about object-oriented programming (OOP), starting with a look at what this term means and an answer to the eternal question, "What is an object?" OOP can seem quite difficult at first. The whole of Chapter 8 is devoted to demystifying it and explaining what makes it so great, and you won't actually deal with much C# code until the very end of the chapter.
Everything changes in Chapter 9, when you put theory into practice and start using OOP in your C# applications. This is where the true power of C# lies. You'll start by looking at how to define classes and interfaces, and then move on to class members (including fields, properties, and methods) in Chapter 10. At the end of that chapter...
System requirements
File format: ePUB
Copy protection: Adobe-DRM (Digital Rights Management)
System requirements:
- Computer (Windows; MacOS X; Linux): Install the free reader Adobe Digital Editions prior to download (see eBook Help).
- Tablet/smartphone (Android; iOS): Install the free app Adobe Digital Editions or the app PocketBook before downloading (see eBook Help).
- E-reader: Bookeen, Kobo, Pocketbook, Sony, Tolino and many more (not Kindle).
The file format ePub works well for novels and non-fiction books – i.e., „flowing” text without complex layout. On an e-reader or smartphone, line and page breaks automatically adjust to fit the small displays.
This eBook uses Adobe-DRM, a „hard” copy protection. If the necessary requirements are not met, unfortunately you will not be able to open the eBook. You will therefore need to prepare your reading hardware before downloading.
Please note: We strongly recommend that you authorise using your personal Adobe ID after installation of any reading software.
For more information, see our ebook Help page.