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 – The Core Language

Book Description

A C# 7 beginners guide to the core parts of the C# language!

About This Book

  • Learn C#, Visual Studio, and Object Oriented Programming,
  • See practical examples of all the core C# language features so that you can easily master them yourself
  • Use the C# 7 programming language to work with code and data, which can be applied to other programming languages as well,
  • Complete a variety of programming assignments for hands-on practice, as you move through the course.

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.

What You Will Learn

  • Learn C#, Visual Studio, and object-oriented programming
  • Learn all the core C# 7 language syntax with hands-on working examples
  • Learn everything from basic variable assignments to complex multidimensional arrays
  • Go through practical examples of all the core C# 7 language features so that you can easily master them yourself
  • Use the C# programming language to work with code and data, which can be applied to other programming languages as well

In Detail

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.

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. You'll also get to work with other tools and technologies that complement C# programming. Each core part of the C# 7 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.

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. Why C# and How to Download and Install the Visual Studio Community Edition
    1. Locating and downloading the Visual Studio Community edition
    2. Summary
  3. Customizing Visual Studio to Make it Feel More Personal
    1. Customizing Visual Studio
    2. Selecting the text editor font and size
    3. Selecting the Statement Completion font and size
    4. Selecting the Environment font and size
    5. Positioning Visual Studio panels
    6. Summary
  4. Creating and Running Your First Page
    1. Starting a new project in Visual C#
    2. Working with Solutions Explorer
    3. Adding a document to your browser
    4. Working with HTML code in Visual Studio
    5. Launching the HTML code
    6. Summary
  5. Creating and Running a Page That Incorporates C#
    1. Improving your first program
    2. Working in the Design view
    3. Working in the Source view
      1. Adding comments
      2. Adding attribute properties
    4. Viewing the code in your browser
    5. Summary
  6. Creating and Using a Single Variable
    1. Setting up Visual C# to deal with a variable
      1. Variables represent change
      2. Declaring a variable
      3. Matching variable types
    2. Running the code
      1. Appending new text
      2. Running the code with the appended text
      3. Embedding a line break
    3. Summary
  7. String Interpolation and Updating Visual Studio
    1. Selecting a NuGet package
      1. Installing a NuGet package
      2. Printing a variable
    2. Summary
  8. Formatting Output Strings for More Professional Results
    1. Setting a value and formatting it as currency
      1. Rounding the output
      2. Formatting a value as a percent
      3. Formatting a value as a date
    2. Summary
  9. Using Variables and Data Types
    1. Setting a page title automatically
      1. Setting a page title so that it can be controlled dynamically
      2. Working with a Boolean data type
      3. Using the decimal data type to set monetary values
    2. Summary
  10. Computed Variables and Basic Math
    1. Declaring and setting two variables to signify two different salaries
      1. Adding the two salaries
      2. Averaging the two salaries
    2. Summary
  11. Interacting with Users Through the Web Page
    1. Using Toolbox to search for a command
      1. Adding an Event Handler
      2. Working with string input
      3. Converting string input to a numerical value
      4. Working with methods
      5. Working with the converted numerical value
    2. Summary
  12. Using Method Chaining to Write More Compact Code
    1. Inserting a text box into the markup
      1. Entering your C# code
      2. Introducing method chaining
      3. Testing your code
    2. Summary
  13. Reacting to a Single Condition with If/Else Blocks
    1. Adding checkbox to your code
      1. Adding a panel statement
      2. Testing the checkbox
      3. Working with the AutoPostBack property
      4. Constructing an if statement
      5. Constructing an else statement
      6. Testing your code
      7. Summary
  14. Making a Variable Grow by Adding 1
    1. Growing a variable by one
      1. Another way to grow a variable
    2. Summary
  15. Repeating Blocks of Code with While Loops
    1. Inserting a button
      1. Modifying the button
      2. Creating a while loop
      3. Working with the Math class
      4. Running the code
      5. Experimenting with the code
    2. Summary
  16. Repeating Blocks of Code with For Loops
    1. Inserting a button
      1. Inserting ListBox
      2. Binding in the code
      3. Starting a for loop
      4. Filling ListBox using the for loop
    2. Summary
  17. Iterating Over Collections with foreach Loops
    1. Inserting TextBox
      1. Inserting a label
      2. Relating TextBox with Label
      3. Generating a vertical output
      4. Removing parts of the code to determine their impact
    2. Summary
  18. Examining Multiple Variable Values with Switch Blocks
    1. Inserting DropBox and Label
      1. Populating the drop-down list
      2. Adding information to display about each drop-down item
      3. Replicating cases
      4. Running your program
    2. Summary
  19. Improving Input Processing with TryParse
    1. Inserting a button and a textbox
      1. Inserting the label
      2. Introducing the TryParse method
      3. Using TryParse to convert input into a numerical value
      4. Running your program
    2. Summary
  20. Replacing If/Else Blocks with the Ternary Operator
    1. Inserting two text boxes and a button
      1. Creating the interface
      2. Combining the ternary operator with TryParse
      3. The Boolean power of TryParse
      4. Running your program
    2. Summary
  21. Operators That Evaluate and Assign in Place
    1. Inserting Button and Label
      1. Introducing operators
        1. Incrementing the value of a variable by 1 with ++
        2. Incrementing the value of a variable by 2 with +=
        3. Working with the -- operator
        4. Working with the == operator
    2. Summary
  22. Checking Two Conditions with the Logical AND Operator
    1. Setting up the project
      1. Building your simulation
      2. Determining if there is something in the text boxes
      3. Displaying the results
    2. Summary
  23. Checking Two Conditions with the Logical OR Operator
    1. Working with check boxes
      1. Introducing the logical || (OR) operator
      2. Experimenting with the logical || (OR) operator
    2. Summary
  24. Declaring, Setting, and Reading Arrays
    1. Setting up the basic interface
      1. Creating the array
      2. Setting up an index
      3. Using arrays to do addition
      4. Using arrays to display an average
      5. Viewing the page source
    2. Summary
  25. Iterating Over Arrays with foreach Loops
    1. Setting up the beginning interface
      1. Creating an array of strings
      2. Introducing the foreach loop
      3. Displaying the results
      4. Running the program
    2. Summary
  26. Creating and Using a Simple Method
    1. Setting up the user interface
      1. Creating a method
      2. Defining the body of the method
      3. Working with the Convert class
      4. Increasing the value of a variable with the GetIncreasedValue method
      5. Outputting the results
      6. Running your program
    2. Summary
  27. Passing Arrays into Methods
    1. Setting up the user interface
      1. Creating the method to find the total of values inside an array
      2. Summing up the values
      3. Using a foreach loop to grab entries inside the array
      4. Adding up the values as the loop operates
      5. Returning the value
      6. Filling the array
      7. Outputting the results
      8. Running your program
    2. Summary
  28. Reference Type and Value Type Variables
    1. Setting up the user interface
      1. Reference type versus value type variables
        1. Reference type variables
        2. Value type variables
    2. Summary
  29. Creating More Flexible Methods with the params Keyword
    1. Designing the starting markup
      1. Creating the product method
      2. Using the product method to multiply variables
    2. Summary
  30. Creating More Flexible Functions with the out Keyword
    1. Setting up the beginning interface
      1. Introducing language-integrated queries
      2. Using the out parameter
      3. Working with the average extension method
      4. Working with the var keyword
      5. Outputting the results of the Summarize method
      6. Running and modifying your program
    2. Summary
  31. Combining the ref and out Keywords to Write Flexible Functions
    1. Setting up the starting interface
      1. Creating the GetValueAndInterest method
      2. The ref and out keywords
      3. Calculating principle and interest
      4. Outputting the results
      5. Running your program
    2. Summary
  32. The out Keyword in C# 7
    1. Creating the program template
    2. Writing the Summarize function
    3. Implementing the function
    4. Running the program
    5. Summary
  33. Multidimensional Arrays
    1. Understanding two-dimensional arrays
      1. Setting up the initial interface
      2. Coding a two-dimensional array
      3. Printing out your results
      4. Running the program
      5. What else can you do?
    2. Summary
  34. Writing Easier Code with the Var and Dynamic Keywords
    1. Setting up the project
      1. Working with the dynamic keyword
      2. Working with the var keyword
      3. Running the program
    2. Summary
  35. Creating a Class with a Constructor and a Function
    1. Our initial markup
      1. Creating a class
      2. Creating the class structure
      3. Working with an instance
      4. Setting the account balance
      5. Displaying the balance using a method
      6. Making a bank account object
      7. Crunching the numbers
      8. Running the program
      9. Private versus public
    2. Summary
  36. Creating a Class with a Static Method
    1. Setting up a simple initial interface
      1. Introducing static classes
      2. Creating the Reverse method
      3. Coding the Reverse method
      4. Reversing the array
      5. Undoing the reversed string
      6. Running the program
    2. Summary
  37. Creating a Class with an Object Property
    1. Setting up a simple markup page
      1. Creating a class
      2. Introducing properties
      3. Using the Circle class
      4. Making a Circle object
    2. Summary
  38. Creating a Class with Static Fields, and Properties
    1. Setting up the interface for this project
      1. Creating the Truck class
      2. Counting trucks
      3. Closing IS Express (Windows users)
      4. Documenting your program
      5. Lesson review
    2. Summary
  39. Centralizing Common Code with Inheritance
    1. Setting up the interface for this lesson
      1. Creating the Vehicles class file
      2. Introducing instance variables
      3. Writing a function to get the make of a vehicle
      4. Creating the Truck class file
      5. Creating the sedan class file
      6. The structure of our program
      7. Designing the interface for user input
      8. Creating an array of strings
      9. Displaying the results
      10. Running the program
      11. Lesson review
    2. Summary
  40. Centralizing Default Code with Virtual Functions
    1. Setting up a basic HTML for this project
      1. Creating the Vehicle function
      2. Writing a virtual function
      3. Using the Virtual function
      4. Overriding a method
      5. Exploring the Vehicle class diagram and the base class object
      6. Building the user interface
      7. Making the Vehicles
      8. Displaying the output
      9. Running the program
      10. Lesson review
    2. Summary
  41. Model Concepts with Abstract Classes
    1. Introducing the basic HTML for this lesson's project
      1. Adding the ThreeDShapes class
      2. Introducing abstract classes
      3. Determining volumes
        1. Calculating the volume of a sphere
      4. Taking advantage of the radius using the static System.Math
        1. Calculating the volume of a cube
      5. Entering data
      6. Making a new sphere
      7. Making a new cube
      8. Displaying the results
      9. Running the program
      10. Lesson review
    2. Summary
  42. Using Custom Types as Return Types
    1. Setting up the project
      1. Creating the SongLibrary class
      2. Sorting SongLibrary
        1. Check it yourself
      3. Displaying the song library
      4. Running the program
      5. Lesson review
    2. Summary
  43. Using Lists to Operate on Data Efficiently
    1. Summarizing the values in a list
      1. Introducing a generic namespace
      2. Starting a list of doubles
      3. Filling the list
      4. Displaying the max, min, average, and sum
      5. Squaring the results
      6. Nesting lists
    2. Summary
  44. Writing Less Code with Polymorphism
    1. Setting up the markup for this project
      1. Creating the product class
        1. Making a book class
        2. Making a Shoe class
      2. Capturing product information
      3. Making a list of products
      4. Displaying the results
      5. Running the program
      6. Chapter review
    2. Summary
  45. Using Interfaces to Express Common Behaviors
    1. Building a hierarchy
      1. Implementing the interface
        1. Check yourself
      2. Setting up the HTML
        1. Check yourself
      3. Building the interface
      4. Running the program
      5. Lesson review
    2. Summary
  46. Iterating over Instances with Indexers
    1. Setting up a records project
      1. Making the indexer
      2. Reading the values back to the user
      3. Making a new Records object
      4. Running the program
      5. Chapter review
    2. Summary
  47. Building Stabler Apps with Exception Handling
    1. Setting up the HTML for this chapter
    2. Using a try block
      1. Throwing an exception
      2. Using a catch block
      3. Using a finally block
      4. Running the program
      5. Working with the StackTrace property
      6. Investigating exceptions to determine their inheritance
      7. Chapter review
    3. Summary
  48. Using Named and Optional Parameters
    1. Setting up the Book class
      1. Making the constructor
      2. Building the search code
      3. Our program interface
      4. Working with lists
      5. Searching a list
      6. Displaying the results
      7. Lesson review
    2. Summary
  49. Using the Null Coalescing Operator to Write Stabler Applications
    1. Setting up the basic HTML for this project
      1. Creating the Car class
    2. Searching a database
      1. Making a list of cars
      2. The null coalescing operation
      3. Displaying the search results
      4. Debugging your program
      5. Running the debugged program
      6. One more fix
      7. Lesson review
    3. Summary
  50. Overloading Operators to Perform Custom Operations
    1. Creating a Vector class
      1. Overloading a mathematical operator
      2. Determining the sum of the vectors
      3. Adding vectors
      4. Making vectors
      5. Displaying the results
      6. Running the program
      7. Lesson review
    2. Summary
  51. Using Enumerations to Represent Named Constants
    1. Setting up a drop-down list
      1. Creating the enumeration for this project
      2. Running the program
      3. Lesson review
    2. Summary
  52. Creating and Using Namespaces
    1. Creating a new project with a new namespace
      1. Making new public classes
      2. Calculating interest
      3. Working with dynamic link libraries
      4. Creating a new website from a template
      5. Determining the amount of interest
      6. Making a library accessible
      7. Summing the values
      8. Running the program
      9. Lesson review
    2. Summary
  53. Structs, Random Points, and Sleeping Threads
    1. Adding a SimpleStruct class file
    2. Coding the struct
    3. Modifying the HTML to make use of the struct
    4. Making an array of points
    5. Displaying the results
    6. Running the program
    7. Debugging the program
    8. Lesson review
    9. Summary
  54. Declaring, Creating, and Using Delegates
    1. Setting up the HTML for this project
    2. Checking out the interface
    3. Making a delegate
    4. Running the program
    5. Summary
  55. Switch Blocks with when in C# 7.0
    1. Creating the classes
    2. Writing the Main function code
    3. Running the program
    4. Summary
  56. Switch Blocks with Objects in C# 7.0
    1. Creating the interface
    2. Implementing the interface
    3. Creating the Main() program
    4. Running the program
    5. Summary
  57. Tuples in C# 7.0
    1. Creating the tuple function
    2. Writing the Main() code
    3. Running the program
    4. Summary
  58. Local functions in C# 7.0
    1. Creating the application and core functions
    2. Creating the local function
    3. Running the code
    4. Summary
  59. Throwing Exceptions in C# 7.0
    1. Creating the main code
      1. Throwing an exception
      2. Catching an exception
    2. Running the program
    3. Summary
  60. Tuples in C# 7.0, Part 2
    1. Creating the constructor
      1. Setting the properties
      2. Writing the Main function
      3. Running the code
    2. Summary