O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Beginning C# 7 Hands-On – Advanced Language Features

Book Description

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

Who This Book Is For

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

In Detail

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 approach

A 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.

Table of Contents

  1. Preface
    1. What you need for this book
    2. Who this book is for
    3. Conventions
    4. Reader feedback
    5. Customer support
      1. Downloading the example code
      2. Downloading the color images of this book
      3. Errata
      4. Piracy
      5. Questions
  2. Creating a Simple Generics Class
    1. Creating a generics class
      1. Working with different data types
      2. Making parameters that are generic
      3. Displaying the values
    2. Adding a button to Default.aspx
    3. Initializing a collection of integers to their array and displaying the results
      1. Changing the data types in our generics class
      2. Running the program
      3. Accumulating the input
    4. Chapter review
    5. Summary
  3. Creating a Generic Method
    1. Creating a button to exchange and then comparing two values
    2. Writing a swap function
      1. Comparing values using the CompareTo method
      2. Introducing constraints
      3. Completing the GenMethods class
      4. Hardcoding the values
      5. Running the program
      6. Modifying the program for an additional type of comparison
      7. Modifying the program for different data types
    3. Chapter review
    4. Summary
  4. Implementing a Generic Interface to Accomplish Sorting
    1. Adding a button to sort and display results
    2. Creating a generics interface class
      1. Implementing the interface
      2. Adding a virtual function
      3. Adding refinements
    3. Entering the reference code
      1. Upcasting
      2. Downcasting
      3. Running the program
    4. Chapter review
    5. Summary
  5. Making Delegates More Flexible with Generics
    1. Adding a summarize button to the HTML
      1. Constructing a delegate
      2. Assigning functions to represent the delegate
      3. Calling the delegate
      4. Running the program
    2. Summary
  6. Creating and Using Generic Dictionaries
    1. Adding a show button to the HTML
    2. Starting a process from a web page
      1. Making a verbatim string
      2. Iterating over key-value pairs
    3. Making a directory and creating a file from Command Prompt
    4. Chapter review
    5. Summary
  7. Connection Between Delegates and Lambda Expressions
    1. Adding a Show Results button to the HTML
    2. Adding delegates
      1. Setting up the variables
      2. Making objects of the delegate type
      3. Defining lambda expressions
      4. Operating an array
      5. Working with actions
    3. Chapter review
    4. Summary
  8. Expression-Bodied Lambdas and Expression-Bodied Members
    1. Adding a box and a Find Max button to the HTML
    2. Making the delegate
      1. Defining an expression-bodied member
      2. Converting an array of strings into an array of doubles
      3. Creating an expression-bodied lambda
      4. Comparing values
      5. Specifying the parameters
      6. Running the program
    3. Chapter review
    4. Summary
  9. Anonymous Methods and Objects That Run Their Own Delegates
    1. Adding a Show Results button to the HTML
    2. Streamlining writing functions
      1. Performing an action on all of the values
      2. Making an anonymous function or method
      3. Starting a thread
      4. Running and modifying the program
    3. Chapter review
    4. Summary
  10. C# with LINQ and Built-In Types
    1. Adding a Show Values button to the HTML
    2. Adding namespaces
      1. Working with the IEnumerable generic interface
      2. Converting an array to a list of integers
      3. Determining the values in a collection
      4. Converting the values back to a list
      5. Extracting values from the list and sorting them
      6. Running the program
    3. Chapter review
    4. Summary
  11. C# with LINQ and Custom Data Types
    1. Adding a Show People button to the HTML
    2. Setting up a database
      1. Making a custom type using LINQ
      2. Setting up an array of people
      3. Querying the array
      4. Running the program
    3. Chapter review
    4. Summary
  12. Using Query Syntax to Construct Queries
    1. Adding a show button to the HTML
    2. Creating a decimal salary array
      1. Working with range variables
      2. Selecting a salary range and putting it in descending order
      3. Displaying the results
    3. Observing deferred execution
      1. Making a dictionary
      2. Working with key-value pairs
      3. Querying the data in the key-value pairs
      4. Running the program
    4. Chapter review
    5. Summary
  13. Queries That Perform Aggregation Functions
    1. Adding a show button to the HTML
    2. Creating an array
      1. Averaging the values in a list
      2. Displaying the results
      3. Using the Count function
      4. Working with a list of lists
      5. Adding students to the classList
      6. Summarizing the information in the classList
      7. Running the program
    3. Chapter review
    4. Summary
  14. Using LINQ to Summarize Tuples
    1. Adding a show tuple summary values button to the HTML
    2. Introducing tuples
      1. Adding namespaces
      2. Making a list with a tuple
      3. Returning the tuples
      4. Making a list of doubles
      5. Summarizing the list
      6. Displaying the results
      7. Running the program
    3. Chapter review
    4. Summary
  15. Summarizing Results with Grouping
    1. Adding a Show Results button to the HTML
    2. Adding namespaces
      1. Creating the student class and defining fields
      2. Making a list of students
      3. Grouping names
      4. Displaying the grouped results
    3. Chapter review
    4. Summary
  16. Joining Datasets with Inner Joins
    1. Adding a Join Classes button to the HTML
    2. Adding the namespaces
      1. Creating the person and car classes
      2. Making person objects
      3. Making car objects
      4. Making a list of owners and their cars
      5. Joining the owners and car lists
      6. Getting and displaying the results
      7. Running the program
    3. Chapter review
    4. Summary
  17. Downloading, Installing, and Running SQL Server 2017
    1. Downloading SQL Server 2017 express
      1. Selecting the installation type
      2. Installing the package
    2. Working with SQL server inside Visual Studio
      1. Creating a SQL server database
      2. Adding and defining a table
      3. Adding fields to the table
      4. Updating the structure of the database
    3. Chapter review
    4. Summary
  18. Writing Code to Manually Connect to a Table and Retrieve Records
    1. Adding a show records button to the HTML
      1. Adding a namespace
    2. Making the connection string
      1. Connecting to SQL Server
      2. Catching exceptions
      3. Displaying errors
      4. Opening the connection
      5. Working with the SQL Server data reader
      6. Running the program
    3. Chapter review
    4. Summary
  19. Inserting Records into Tables Using Stored Procedures
    1. Adding text boxes and a button to the HTML
    2. Reviewing what you created already in SQL Server
      1. Creating a new stored procedure
      2. Updating the database structure
      3. Adding a namespace
      4. Building the connection string
      5. Initializing the connection
      6. Catching exceptions
      7. Trying out commands
      8. Adding the parameters
      9. Saving the information for later retrieval
      10. Recognizing the role of indexers
      11. Running the program
    3. Chapter review
    4. Summary
  20. Using the Nullable Feature to Make Apps More Stable
    1. Adding a Show People button to the HTML
      1. Adding a field to the people database
      2. Modifying the dbo.People table
      3. Writing the code for this project
      4. Creating the person class
      5. Making the properties
      6. Making the list of people
      7. Building the connection string
      8. Entering the SQL-related code
      9. Adding people to the list from the table
      10. Displaying the records
      11. Running the program
    2. Chapter review
    3. Summary
  21. Connecting a Chart Control to SQL Server
    1. Placing a chart into the HTML page
      1. Adding a button to the HTML page
    2. Adding a new table to the People database
      1. Enabling auto-incrementation
      2. Adding values to the new table
    3. Coding the project
      1. Adding a namespace
      2. Building the connection string
      3. Writing a SQL query
      4. Making the command object
      5. Opening the connection and making a SQL data reader
      6. Running the program
      7. Modifying the program to display the Y values
    4. Chapter review
    5. Summary
  22. Using LINQ to Operate on Tables from SQL Server
    1. Changing the data in the ExperimentValues table
      1. Summarizing the fields
      2. Adding the namespaces
      3. Building the connection string
      4. Making the SQL connection
      5. Making an adapter
      6. Making a data table
      7. Populating the table with data
      8. Displaying the summed values
      9. Running the program
      10. Adding comments
    2. Chapter review
    3. Summary
  23. Creating a Page That Saves Text to Disk
    1. Creating an application to save text
      1. Creating the user interface for your project
      2. Starting to code the project
      3. Catching an exception
      4. Creating a StreamWriter class
      5. Creating a StreamReader class
      6. Running the program
    2. Chapter review
    3. Summary
  24. Creating a Page That Uses the File Upload Control
    1. Starting our project from scratch
      1. Adding a namespace
      2. Saving a file to a specific location
      3. Saving the file
      4. Displaying messages to the user
      5. Determining which files are stored in a directory
      6. Determining the return type
      7. Exploring the exceptions for EnumerateFiles
      8. Catching the exceptions
      9. Running the program
    2. Chapter review
    3. Summary
  25. Serializing and Deserializing Objects
    1. Adding two buttons to the HTML
      1. Beginning to code the project
      2. Adding namespaces
      3. Creating a serializable class
      4. Adding features to the serializable class
      5. Defining the path for saving a file
      6. Making a Person object
      7. Handling unmanaged resources
      8. Making a binary formatter
      9. Serializing an object
      10. Testing the program
      11. Rebuilding an object from a hard drive
      12. Displaying the results
      13. Running the program
    2. Chapter review
    3. Summary
  26. Having a Little Fun with Images with Pixel Manipulations
    1. Manipulating an image
      1. Adding a button and an image control to the HTML
      2. Adding a namespace
      3. Making a bitmap
      4. Saving an image as a bitmap picture
      5. Accessing a pixel's location
      6. Manipulating the pixels
      7. Converting the picture to a byte array
      8. Sending out the image URL
      9. Running the program
    2. Chapter review
    3. Summary
  27. Saving an Image to SQL Server
    1. Adding buttons and a list box to HTML
      1. Creating a database table to store the files
      2. Storing image files on your hard drive
      3. Adding namespaces
      4. Coding the application
      5. Testing the scanning the folder function
      6. Building the connection string
      7. Using of the connection string
      8. Running the program
    2. Chapter review
    3. Summary
  28. Creating and Using an XML File
    1. Adding a button to HTML
      1. Coding the XML
      2. Adding a namespace
      3. Loading the XML file into your program
      4. Iterating over the contents of the XML file
      5. Displaying the results
      6. Running the program
    2. Chapter review
    3. Summary
  29. Creating XML Files with C#
    1. Adding a button to HTML
      1. Adding namespaces
      2. Coding the XmlWriter settings
      3. Writing to the hard drive
      4. Formatting the results
      5. Running the program
    2. Chapter review
    3. Summary
  30. Querying XML Documents with LINQ
    1. Adding a text box and a button to HTML
      1. Adding namespaces
      2. Clearing the output
      3. Building an element tree
      4. Saving the store XML file
      5. Testing the program
      6. Searching for items that meet a specific criteria
      7. Selecting among the items that meet the search criteria
      8. Displaying the results
      9. Running the program
    2. Chapter review
    3. Summary