C# Essentials
Total Page:16
File Type:pdf, Size:1020Kb
C# Essentials C# 4.0 Essentials 2 C# Essentials C# 4.0 Essentials – Version 1.04 © 2010 Payload Media. This eBook is provided for personal use only. Unauthorized use, reproduction and/or distribution strictly prohibited. All rights reserved. The content of this book is provided for informational purposes only. Neither the publisher nor the author offers any warranties or representation, express or implied, with regard to the accuracy of information contained in this book, nor do they accept any liability for any loss or damage arising from any errors or omissions. Find more technology eBooks at www.ebookfrenzy.com 3 C# Essentials Table of Contents Chapter 1. About C# Essentials ................................................................................................. 12 Chapter 2. The C# Language and Environment ........................................................................ 13 2.1 A Brief History of Computer Programming Languages .................................................. 13 2.2 What exactly is C#? ........................................................................................................ 14 2.3 The Common Language Infrastructure (CLI) .................................................................. 14 2.4 Common Intermediate Language (CIL) .......................................................................... 15 2.5 Virtual Execution System (VES) ...................................................................................... 15 2.6 Common Type System (CTS) & Common Language Specification (CLS) ........................ 16 2.7 The Framework (Base Class and Framework Class Libraries) ........................................ 16 2.8 Non Microsoft Implementations of the CLI ................................................................... 16 Chapter 3. A Simple C# Console Application ............................................................................ 17 3.1 Options for Installing a C# Environment ........................................................................ 17 3.2 C# Source File Naming Conventions .............................................................................. 18 3.3 Creating a Sample C# Program ....................................................................................... 18 3.4 Compiling C# Code from the Command Line ................................................................. 18 3.5 Executing a Compiled C# Program ................................................................................. 19 Chapter 4. Creating a Simple C# GUI Application with Visual Studio ....................................... 20 4.1 Installing Visual Studio with C# Support ........................................................................ 20 4.2 Creating a new Visual Studio C# Project ........................................................................ 20 4.3 Adding Components to the Windows Form................................................................... 20 4.4 Changing Component Names ......................................................................................... 21 4.5 Changing Component Properties ................................................................................... 22 4.6 Adding Behavior to a Visual Studio C# Application ........................................................ 23 Chapter 5. C# Variables and Constants .................................................................................... 25 5.1 What is a C# Variable?.................................................................................................... 25 5.2 What is a C# Constant? .................................................................................................. 26 5.3 C# Integer Variable Types .............................................................................................. 26 4 C# Essentials 5.4 C# Floating Point Variables ............................................................................................ 27 5.5 The C# Decimal Variable Type ........................................................................................ 27 5.6 C# Boolean Variable Type .............................................................................................. 28 5.7 C# Character Variable Type ............................................................................................ 28 5.8 C# String Variables ......................................................................................................... 29 5.9 Casting Variable Types in C# .......................................................................................... 29 Chapter 6. C# Operators and Expressions ................................................................................ 31 6.1 What is an Expression? .................................................................................................. 31 6.2 The Basic Assignment Operator ..................................................................................... 31 6.3 C# Arithmetic Operators ................................................................................................ 32 6.4 C# Operator Precedence ................................................................................................ 33 6.5 Compound Assignment Operators ................................................................................. 34 6.6 Increment and Decrement Operators ............................................................................ 35 6.7 Comparison Operators ................................................................................................... 36 6.8 Boolean Logical Operators ............................................................................................. 37 6.9 The Ternary Operator..................................................................................................... 38 Chapter 7. C# Flow Control with if and else ............................................................................. 39 7.1 Using the if Statement ................................................................................................... 39 7.2 Using if ... else .. Statements .......................................................................................... 40 7.3 Using if ... else if .. Statements ....................................................................................... 40 7.4 Summary ........................................................................................................................ 41 Chapter 8. The C# switch Statement ........................................................................................ 42 8.1 Why Use a switch Statement? ....................................................................................... 42 8.2 Using the switch Statement Syntax ............................................................................... 43 8.3 A switch Statement Example ......................................................................................... 44 8.4 Explaining the Example .................................................................................................. 45 8.5 Using goto in a C# switch Statement ............................................................................. 45 8.6 Using continue in a C# switch Statement ...................................................................... 47 5 C# Essentials Chapter 9. C# Looping - The for Statement .............................................................................. 48 9.1 Why Use Loops? ............................................................................................................. 48 9.2 C# Loop Variable Scope .................................................................................................. 50 9.3 Creating an Infinite for Loop .......................................................................................... 50 9.4 Breaking Out of a for Loop ............................................................................................. 50 9.5 Nested for Loops ............................................................................................................ 51 9.6 Breaking From Nested Loops ......................................................................................... 51 9.7 Continuing for Loops ...................................................................................................... 52 Chapter 10. C# Looping with do and while Statements ............................................................. 53 10.1 The C# while Loop .......................................................................................................... 53 10.2 C# do ... while loops ....................................................................................................... 54 10.3 Breaking from Loops ...................................................................................................... 54 10.4 The continue Statement ................................................................................................. 55 Chapter 11. C# Object Oriented Programming .......................................................................... 57 11.1 What is an Object? ......................................................................................................... 57 11.2 What is a Class? .............................................................................................................. 57 11.3 Declaring a C# Class .......................................................................................................