Beginning C# 7 Hands-On – Advanced Language Features: Learn the advanced-level features of C# 7 using Visual Studio 2017
By Tom Owsiak
()
About this ebook
An advanced C# beginners guide to some of the tougher parts of the C# language!
About This Book- Learn C#, Visual Studio, and object-oriented programming
- Get practical examples of advanced C# language features so that you can easily master them yourself
- Use the C# programming language to work generics and lambda expressions
- Program C# and SQL Server 2017
- Get ready for your first MVC applications
This book is for anyone who is interested in learning how to program the more advanced aspects of the C# language. Previous programming knowledge of C# is required, at least to the level of basic object-oriented programming. Readers can benefit from first reading Tom Owsiak's companion book, Beginning C# 7 Hands-On – The Core Language, as a preparation for the more advanced elements and techniques presented in this book.
What You Will Learn- Learn C# advanced language elements and techniques
- Discover advanced C# techniques with hands-on working examples
- Build on your knowledge of OOP by using C# generics and lambda expressions
- Work with C# and LINQ custom data types
- Program C# and SQL Server 2017 to manage and query data
- Create your first full MVC application
Beginning C# 7 Hands-On – Advanced Language Features assumes that you've mastered the basic elements of the C# language and that you're now ready to learn the more advanced C# language and syntax, line by line, in a working Visual Studio environment. You'll learn how to code advanced C# language topics including generics, lambda expressions, and anonymous methods.
You'll learn to use query syntax to construct queries and deploy queries that perform aggregation functions. Work with C# and SQL Server 2017 to perform complex joins and stored procedures. Explore advanced file access methods, and see how to serialize and deserialize objects – all by writing working lines of code that you can run within Visual Studio.
This book is designed for beginner C# developers who have mastered the basics now, and anyone who needs a fast reference to using advanced C# language features in practical coding examples. You'll also take a look at C# through web programming with web forms.
By the time you've finished this book, you'll know all the critical advanced elements of the C# language and how to program everything from C# generics to XML, LINQ, and your first full MVC web applications. These are the advanced building blocks that you can then combine to exploit the full power of the C# programming language, line by line.
Style and approachA comprehensive book that blends theory with just the right amount of practical code implementations, to help you get up and running with the C# programming language and its advanced features. You'll also get to work with other tools and technologies that complement C# programming. Each core part of the C# language is coded as you learn, and code output is tested every time to verify the syntax is working as expected, so it's easy for you to learn directly from the working code examples. Advanced-level features of C# will be used to code and work through examples.
Related to Beginning C# 7 Hands-On – Advanced Language Features
Related ebooks
Beginning C# 7 Hands-On – The Core Language Rating: 0 out of 5 stars0 ratingsMastering ASP.NET Core 2.0: MVC patterns, configuration, routing, deployment, and more Rating: 0 out of 5 stars0 ratingsBeginning C++ Programming Rating: 3 out of 5 stars3/5Functional C# Rating: 5 out of 5 stars5/5ASP.NET Core 3 and React: Hands-On full stack web development using ASP.NET Core, React, and TypeScript 3 Rating: 0 out of 5 stars0 ratingsC++17 By Example: Practical projects to get you up and running with C++17 Rating: 0 out of 5 stars0 ratingsWeb Design Blueprints Rating: 0 out of 5 stars0 ratingsASP.NET Core 5 and React: Full-stack web development using .NET 5, React 17, and TypeScript 4 Rating: 0 out of 5 stars0 ratingsC# 7 and .NET Core 2.0 Blueprints: Build effective applications that meet modern software requirements Rating: 0 out of 5 stars0 ratingsASP.NET Core 2 Fundamentals: Build cross-platform apps and dynamic web services with this server-side web application framework Rating: 0 out of 5 stars0 ratingsMastering Windows Presentation Foundation: Build responsive UIs for desktop applications with WPF, 2nd Edition Rating: 0 out of 5 stars0 ratingsLearning Google Apps Script Rating: 0 out of 5 stars0 ratingsJavaScript by Example Rating: 0 out of 5 stars0 ratingsLearning Bootstrap 4 - Second Edition Rating: 0 out of 5 stars0 ratingsJava Programming for Beginners: Learn the fundamentals of programming with Java Rating: 0 out of 5 stars0 ratingsMastering macOS Programming Rating: 0 out of 5 stars0 ratingsEffective Amazon Machine Learning Rating: 0 out of 5 stars0 ratingsMastering Visual Studio 2017 Rating: 0 out of 5 stars0 ratingsCross-platform Desktop Application Development: Electron, Node, NW.js, and React Rating: 0 out of 5 stars0 ratingsLearning jQuery 3 - Fifth Edition Rating: 0 out of 5 stars0 ratingsBootstrap for ASP.NET MVC - Second Edition Rating: 5 out of 5 stars5/5Apps and Services with .NET 8: Build practical projects with Blazor, .NET MAUI, gRPC, GraphQL, and other enterprise technologies Rating: 0 out of 5 stars0 ratingsFuelPHP Application Development Blueprints Rating: 0 out of 5 stars0 ratingsTypeScript 2.x By Example: Build engaging applications with TypeScript, Angular, and NativeScript on the Azure platform Rating: 0 out of 5 stars0 ratingsLearning JavaScript Data Structures and Algorithms: Write complex and powerful JavaScript code using the latest ECMAScript Rating: 0 out of 5 stars0 ratings
Programming For You
Excel : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Excel Programming: 1 Rating: 5 out of 5 stars5/5Excel 101: A Beginner's & Intermediate's Guide for Mastering the Quintessence of Microsoft Excel (2010-2019 & 365) in no time! Rating: 0 out of 5 stars0 ratingsSQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5Coding All-in-One For Dummies Rating: 4 out of 5 stars4/5Grokking Algorithms: An illustrated guide for programmers and other curious people Rating: 4 out of 5 stars4/5Python QuickStart Guide: The Simplified Beginner's Guide to Python Programming Using Hands-On Projects and Real-World Applications Rating: 0 out of 5 stars0 ratingsPython Programming : How to Code Python Fast In Just 24 Hours With 7 Simple Steps Rating: 4 out of 5 stars4/5HTML & CSS: Learn the Fundaments in 7 Days Rating: 4 out of 5 stars4/5Python Machine Learning By Example Rating: 4 out of 5 stars4/5Python: For Beginners A Crash Course Guide To Learn Python in 1 Week Rating: 4 out of 5 stars4/5Learn to Code. Get a Job. The Ultimate Guide to Learning and Getting Hired as a Developer. Rating: 5 out of 5 stars5/5SQL: For Beginners: Your Guide To Easily Learn SQL Programming in 7 Days Rating: 5 out of 5 stars5/5Learn PowerShell in a Month of Lunches, Fourth Edition: Covers Windows, Linux, and macOS Rating: 5 out of 5 stars5/5PYTHON: Practical Python Programming For Beginners & Experts With Hands-on Project Rating: 5 out of 5 stars5/5JavaScript All-in-One For Dummies Rating: 5 out of 5 stars5/5Unreal Engine from Zero to Proficiency (Foundations): Unreal Engine from Zero to Proficiency, #1 Rating: 3 out of 5 stars3/5Python: Learn Python in 24 Hours Rating: 4 out of 5 stars4/5Linux: Learn in 24 Hours Rating: 5 out of 5 stars5/5C# Programming from Zero to Proficiency (Beginner): C# from Zero to Proficiency, #2 Rating: 0 out of 5 stars0 ratingsSpies, Lies, and Algorithms: The History and Future of American Intelligence Rating: 4 out of 5 stars4/5
Reviews for Beginning C# 7 Hands-On – Advanced Language Features
0 ratings0 reviews
Book preview
Beginning C# 7 Hands-On – Advanced Language Features - Tom Owsiak
Beginning C# 7 Hands-On – Advanced Language Features
Learn the advanced-level features of C# 7 using
Visual Studio 2017
Tom Owsiak
BIRMINGHAM - MUMBAI
Beginning C# 7 Hands-On – Advanced Language Features
Copyright © 2017 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: October 2017
Production reference: 1271017
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham
B3 2PB, UK.
ISBN 978-1-78829-426-3
www.packtpub.com
Credits
About the Author
Tom Owsiak has eight years of experience as a teacher in Mathematics, Physics, Statistics, and Programming. He has worked for five years as a database programmer using various technologies such as .NET, Clipper, SQL, SQL Server, SAS, and Excel, and many related technologies.
Tom is the publisher of one of the most successful courses on Udemy, called Learn C# With Visual Studio 2013. Currently, Tom works as a Mathematics and Computer Science teacher at Mercy College in Dobbs Ferry, NY.
www.PacktPub.com
For support files and downloads related to your book, please visit www.PacktPub.com. Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details. At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.
https://www.packtpub.com
Get the most in-demand software skills with Mapt. Mapt gives you full access to all Packt books and video courses, as well as industry-leading tools to help you plan your personal development and advance your career.
Why subscribe?
Fully searchable across every book published by Packt
Copy and paste, print, and bookmark content
On demand and accessible via a web browser
Customer Feedback
Thanks for purchasing this Packt book. At Packt, quality is at the heart of our editorial process. To help us improve, please leave us an honest review on this book's Amazon page at https://www.amazon.com/dp/1788294262.
If you'd like to join our team of regular reviewers, you can email us at [email protected]. We award our regular reviewers with free eBooks and videos in exchange for their valuable feedback. Help us be relentless in improving our products!
Table of Contents
Preface
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Downloading the color images of this book
Errata
Piracy
Questions
Creating a Simple Generics Class
Creating a generics class
Working with different data types
Making parameters that are generic
Displaying the values
Adding a button to Default.aspx
Initializing a collection of integers to their array and displaying the results
Changing the data types in our generics class
Running the program
Accumulating the input
Chapter review
Summary
Creating a Generic Method
Creating a button to exchange and then comparing two values
Writing a swap function
Comparing values using the CompareTo method
Introducing constraints
Completing the GenMethods class
Hardcoding the values
Running the program
Modifying the program for an additional type of comparison
Modifying the program for different data types
Chapter review
Summary
Implementing a Generic Interface to Accomplish Sorting
Adding a button to sort and display results
Creating a generics interface class
Implementing the interface
Adding a virtual function
Adding refinements
Entering the reference code
Upcasting
Downcasting
Running the program
Chapter review
Summary
Making Delegates More Flexible with Generics
Adding a summarize button to the HTML
Constructing a delegate
Assigning functions to represent the delegate
Calling the delegate
Running the program
Summary
Creating and Using Generic Dictionaries
Adding a show button to the HTML
Starting a process from a web page
Making a verbatim string
Iterating over key-value pairs
Making a directory and creating a file from Command Prompt
Chapter review
Summary
Connection Between Delegates and Lambda Expressions
Adding a Show Results button to the HTML
Adding delegates
Setting up the variables
Making objects of the delegate type
Defining lambda expressions
Operating an array
Working with actions
Chapter review
Summary
Expression-Bodied Lambdas and Expression-Bodied Members
Adding a box and a Find Max button to the HTML
Making the delegate
Defining an expression-bodied member
Converting an array of strings into an array of doubles
Creating an expression-bodied lambda
Comparing values
Specifying the parameters
Running the program
Chapter review
Summary
Anonymous Methods and Objects That Run Their Own Delegates
Adding a Show Results button to the HTML
Streamlining writing functions
Performing an action on all of the values
Making an anonymous function or method
Starting a thread
Running and modifying the program
Chapter review
Summary
C# with LINQ and Built-In Types
Adding a Show Values button to the HTML
Adding namespaces
Working with the IEnumerable generic interface
Converting an array to a list of integers
Determining the values in a collection
Converting the values back to a list
Extracting values from the list and sorting them
Running the program
Chapter review
Summary
C# with LINQ and Custom Data Types
Adding a Show People button to the HTML
Setting up a database
Making a custom type using LINQ
Setting up an array of people
Querying the array
Running the program
Chapter review
Summary
Using Query Syntax to Construct Queries
Adding a show button to the HTML
Creating a decimal salary array
Working with range variables
Selecting a salary range and putting it in descending order
Displaying the results
Observing deferred execution
Making a dictionary
Working with key-value pairs
Querying the data in the key-value pairs
Running the program
Chapter review
Summary
Queries That Perform Aggregation Functions
Adding a show button to the HTML
Creating an array
Averaging the values in a list
Displaying the results
Using the Count function
Working with a list of lists
Adding students to the classList
Summarizing the information in the classList
Running the program
Chapter review
Summary
Using LINQ to Summarize Tuples
Adding a show tuple summary values button to the HTML
Introducing tuples
Adding namespaces
Making a list with a tuple
Returning the tuples
Making a list of doubles
Summarizing the list
Displaying the results
Running the program
Chapter review
Summary
Summarizing Results with Grouping
Adding a Show Results button to the HTML
Adding namespaces
Creating the student class and defining fields
Making a list of students
Grouping names
Displaying the grouped results
Chapter review
Summary
Joining Datasets with Inner Joins
Adding a Join Classes button to the HTML
Adding the namespaces
Creating the person and car classes
Making person objects
Making car objects
Making a list of owners and their cars
Joining the owners and car lists
Getting and displaying the results
Running the program
Chapter review
Summary
Downloading, Installing, and Running SQL Server 2017
Downloading SQL Server 2017 express
Selecting the installation type
Installing the package
Working with SQL server inside Visual Studio
Creating a SQL server database
Adding and defining a table
Adding fields to the table
Updating the structure of the database
Chapter review
Summary
Writing Code to Manually Connect to a Table and Retrieve Records
Adding a show records button to the HTML
Adding a namespace
Making the connection string
Connecting to SQL Server
Catching exceptions
Displaying errors
Opening the connection
Working with the SQL Server data reader
Running the program
Chapter review
Summary
Inserting Records into Tables Using Stored Procedures
Adding text boxes and a button to the HTML
Reviewing what you created already in SQL Server
Creating a new stored procedure
Updating the database structure
Adding a namespace
Building the connection string
Initializing the connection
Catching exceptions
Trying out commands
Adding the parameters
Saving the information for later retrieval
Recognizing the role of indexers
Running the program
Chapter review
Summary
Using the Nullable Feature to Make Apps More Stable
Adding a Show People button to the HTML
Adding a field to the people database
Modifying the dbo.People table
Writing the code for this project
Creating the person class
Making the properties
Making the list of people
Building the connection string
Entering the SQL-related code
Adding people to the list from the table
Displaying the records
Running the program
Chapter review
Summary
Connecting a Chart Control to SQL Server
Placing a chart into the HTML page
Adding a button to the HTML page
Adding a new table to the People database
Enabling auto-incrementation
Adding values to the new table
Coding the project
Adding a namespace
Building the connection string
Writing a SQL query
Making the command object
Opening the connection and making a SQL data reader
Running the program
Modifying the program to display the Y values
Chapter review
Summary
Using LINQ to Operate on Tables from SQL Server
Changing the data in the ExperimentValues table
Summarizing the fields
Adding the namespaces
Building the connection string
Making the SQL connection
Making an adapter
Making a data table
Populating the table with data
Displaying the summed values
Running the program
Adding comments
Chapter review
Summary
Creating a Page That Saves Text to Disk
Creating an application to save text
Creating the user interface for your project
Starting to code the project
Catching an exception
Creating a StreamWriter class
Creating a StreamReader class
Running the program
Chapter review
Summary
Creating a Page That Uses the File Upload Control
Starting our project from scratch
Adding a namespace
Saving a file to a specific location
Saving the file
Displaying messages to the user
Determining which files are stored in a directory
Determining the return type
Exploring the exceptions for EnumerateFiles
Catching the exceptions
Running the program
Chapter review
Summary
Serializing and Deserializing Objects
Adding two buttons to the HTML
Beginning to code the project
Adding namespaces
Creating a serializable class
Adding features to the serializable class
Defining the path for saving a file
Making a Person object
Handling unmanaged resources
Making a binary formatter
Serializing an object
Testing the program
Rebuilding an object from a hard drive
Displaying the results
Running the program
Chapter review
Summary
Having a Little Fun with Images with Pixel Manipulations
Manipulating an image
Adding a button and an image control to the HTML
Adding a namespace
Making a bitmap
Saving an image as a bitmap picture
Accessing a pixel's location
Manipulating the pixels
Converting the picture to a byte array
Sending out the image URL
Running the program
Chapter review
Summary
Saving an Image to SQL Server
Adding buttons and a list box to HTML
Creating a database table to store the files
Storing image files on your hard drive
Adding namespaces
Coding the application
Testing the scanning the folder function
Building the connection string
Using of the connection string
Running the program
Chapter review
Summary
Creating and Using an XML File
Adding a button to HTML
Coding the XML
Adding a namespace
Loading the XML file into your program
Iterating over the contents of the XML file
Displaying the results
Running the program
Chapter review
Summary
Creating XML Files with C#
Adding a button to HTML
Adding namespaces
Coding the XmlWriter settings
Writing to the hard drive
Formatting the results
Running the program
Chapter review
Summary
Querying XML Documents with LINQ
Adding a text box and a button to HTML
Adding namespaces
Clearing the output
Building an element tree
Saving the store XML file
Testing the program
Searching for items that meet a specific criteria
Selecting among the items that meet the search criteria
Displaying the results
Running the program
Chapter review
Summary
Preface
Beginning C# 7 Hands-On – Advanced Language Features assumes that you’ve mastered the basic elements of the C# language and that you're now ready to learn the more advanced C# language and syntax, line by line, in a working Visual Studio environment. You'll learn how to code advanced C# language topics including generics, lambda expressions, and anonymous methods. You'll learn to use query syntax to construct queries and deploy queries that perform aggregation functions. You'll work with C# 7 and SQL Server 2017 to perform complex joins and stored procedures. Explore advanced file access methods, and see how to serialize and deserialize objects—all by writing working lines of code that you can run within Visual Studio. You'll also take a look at C# through web programming with web forms. By the time you’ve finished this book, you’ll know all the critical advanced elements of the C# language and how to program everything from C# generics to XML, LINQ, and your first full MVC web applications. These are the advanced building blocks that you can then combine to exploit the full power of the C# programming language, line by line. This book is designed for beginner C# developers who have mastered the basics now, and for anyone who needs a fast reference to using advanced C# language features in practical coding examples.
What you need for this book
Visual Studio 2017, which will install and run on Windows 7 or above, and 2 GB or 4 GB of RAM is recommended. A minimum 20-50 GB of hard disk space is essential for typical installations.
Who this book is for
This book will appeal to anyone who is interested in learning how to program in C#. Previous programming experience will help you get through the initial sections with ease, although it's not mandatory to possess any experience at all.
Conventions
In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning. Code words in text, database table names, folder names, filenames, file extensions, path names, dummy URLs, user input, and Twitter handles are shown as follows: Specifically, Default.aspx is a file that contains the markup of the elements on the web page.
A block of code is set as follows:
server
AutoPostBack=True
>
When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:
server
AutoPostBack=True
>
New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: If you wish, click on Browse and save the file to a location you choose and click on OK.
Warnings or important notes appear like this.
Tips and tricks appear like this.
Reader feedback
Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of. To send us general feedback, simply email [email protected], and mention the book's title in the subject of your message. If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.
Customer support
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
Downloading the example code
You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files emailed directly to you. You can download the code files by following these steps:
Log in or register to our website using your email address and password.
Hover the mouse pointer on the SUPPORT tab at the top.
Click on Code Downloads & Errata.
Enter the name of the book in the Search box.
Select the book for which you're looking to download the code files.
Choose from the drop-down menu where you purchased this book from.
Click on Code Download.
Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:
WinRAR / 7-Zip for Windows
Zipeg / iZip / UnRarX for Mac
7-Zip / PeaZip for Linux
The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Beginning-CSharp-7-Hands-On-Advanced-Language-Features. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!
Downloading