C For Beginners Crash Course Master C Programming Fast and Easy Today 1st Edition Raj Ali PDF Download
C For Beginners Crash Course Master C Programming Fast and Easy Today 1st Edition Raj Ali PDF Download
https://ebookname.com/product/c-for-beginners-crash-course-
master-c-programming-fast-and-easy-today-1st-edition-raj-ali/
Get the full ebook with Bonus Features for a Better Reading Experience on ebookname.com
Instant digital products (PDF, ePub, MOBI) available
Download now and explore formats that suit you...
https://ebookname.com/product/php-5-fast-easy-web-
development-1st-edition-julie-c-meloni/
https://ebookname.com/product/c-programming-in-easy-steps-fourth-
edition-mike-mcgrath/
https://ebookname.com/product/c-python-for-beginners-6th-edition-
david-hayward/
https://ebookname.com/product/how-to-castrate-a-bull-unexpected-
lessons-on-risk-growth-and-success-in-business-1st-edition-dave-
hitz/
Voice Over IP Systems and Solutions Btexact
Communications Technologies Series 3 Richard Swale
https://ebookname.com/product/voice-over-ip-systems-and-
solutions-btexact-communications-technologies-series-3-richard-
swale/
https://ebookname.com/product/race-and-the-rise-of-standard-
american-thomas-paul-bonfiglio/
https://ebookname.com/product/career-coaching-an-insider-s-guide-
marcia-bench/
https://ebookname.com/product/on-taqlid-ibn-al-qayyim-s-critique-
of-authority-in-islamic-law-abdul-rahman-mustafa/
https://ebookname.com/product/roberto-clemente-the-great-
hispanic-heritage-1st-edition-susan-muaddi-darraj/
Symmetrization in Analysis Albert Baernstein Ii
https://ebookname.com/product/symmetrization-in-analysis-albert-
baernstein-ii/
C#
FOR BEGINNERS
CRASH COURSE
Master C# Programming
Fast and Easy Today
By
RAJ ALI
© Copyright 2014 - All rights reserved.
The information provided herein is stated to be truthful and consistent, in that any
liability, in terms of inattention or otherwise, by any usage or abuse of any policies,
processes, or directions contained within is the solitary and utter responsibility of the
recipient reader. Under no circumstances will any legal responsibility or blame be held
against the publisher for any reparation, damages, or monetary loss due to the
information herein, either directly or indirectly.
Respective authors own all copyrights not held by the publisher.
Legal Notice:
This book is copyright protected. This is only for personal use. You cannot amend,
distribute, sell, use, quote or paraphrase any part or the content within this book without
the consent of the author or copyright owner. Legal action will be pursued if this is
breached.
Disclaimer Notice:
Please note the information contained within this document is for educational and
entertainment purposes only. Every attempt has been made to provide accurate, up to
date and reliable complete information. No warranties of any kind are expressed or
implied. Readers acknowledge that the author is not engaging in the rendering of legal,
financial, medical or professional advice.
By reading this document, the reader agrees that under no circumstances are we
responsible for any losses, direct or indirect, which are incurred as a result of the use of
information contained within this document, including, but not limited to, —errors,
omissions, or inaccuracies.
Table of Contents
Chapter 1 Introduction to C#
1.1 Overview of C#
1.2 Programming features of C#
1.3 C# Environment
Components of .NET framework
Chapter 2 Program Structure in C#
2.1 Introduction to C# Program structure
Namespace declaration
Class
Class Methods
Comments
2.2 User Interface elements
Start Page
Standard Toolbar
Solution Explorer
Output window
Error List
Class View Window
Code Editor
2.3 Compiling and executing C# program
Chapter 3 Syntax, Data Types, and conversion
3.1 Different keywords in C#
1) Keywords for class, method, field and property
2) Keywords for type conversions
3) Keywords useful for program flow control
4) Keywords used for built in types and enumerations
5) Keywords used for exception handling
6) Keywords used as literals, method passing parameters
7) Keywords useful in function pointers, object allocation, unmanaged
code
3.2 Data Types in C#
3.3 Type conversion in C#
Implicit type conversion
Explicit type conversion
Chapter 4 Variables and Constants
4.1 Exploring variables in C#
4.2 Constants and literals in C#
Chapter 5 Operators in C#
5.1 Introduction to operators
5.2 Arithmetic operators
5.3 Relational operators
5.4 Logical operators
5.5 Bitwise operators
5.6 Assignment operators
5.7 Miscellaneous operators
Chapter 6 C# Decision making statements
6.1 If Statement
6.2 If else statement
6.3 Nested if statement
6.4 Switch statement
6.5 Nested switch statement
Chapter 7 Loops in C#
7.1 While loop
7.2 For loop
7.3 Do while loop
7.4 Break statement
7.5 Continue statement
Chapter 8 Classes and Methods in C#
8.1 Class declaration
C# constructors
C# destructors
8.2 Defining methods
8.3 Calling methods
8.5 Recursive method call
8.4 Passing parameters to method
Chapter 9 Arrays in C#
9.1 Introduction to arrays
9.2 Arrays declaration
9.3 Initializing and adding values
9.4 Accessing array elements
9.5 Foreach loop
9.6 Different C# arrays
Chapter 10 Strings in C#
10.1 Creation of string
10.2 Properties and methods of string class
10.3 Examples demonstrating the string functionality
Chapter 11 Encapsulation and Polymorphism
11.1 Introduction to encapsulation
11.2 Access specifier in C#
11.3 Polymorphism
11.4 Static Polymorphism
11.5 Dynamic Polymorphism
Chapter 12 Inheritance and Interfaces
12.1 Introduction to Inheritance
12.2 Base and derived classes
12.3 Base class initialization
12.4 Interfaces in C#
12.5 Multiple inheritance in C#
Chapter 13 Operator overloading and exception handling
13.1 Introduction to Operator Overloading
13.2 Different operators in overloading
13.3 Introduction to exception handling
13.4 Exception classes in C#
13.5 Exception handling
13.6 User defined exceptions
Chapter 14 Multithreading
14.1 Thread in C#
14.2 Life cycle of a thread
14.3 Main thread
14.4 Properties and methods of the Thread class
14.5 Creating and managing threads
14.6 Destroying threads
Reference links on C#
Conclusion
Chapter 1 Introduction to C#
1.1 Overview of C#
C# is an object oriented, type safe high level programming language. It has been
developed by Microsoft during the development of the .NET framework. C# was
developed for the Common Language Infrastructure (CLI), this infrastructure was
created to allow programs from various other high level languages to work together
without the need to rewrite those programs entirely. The CLI contains a various
executable programs referred simply as executables and are housed and ran in a system
called the runtime environment.
All the programs created in .Net framework execute in an environment that handles the
runtime requirements. The Common Language Runtime (CLR) provides the virtual
machine, which helps the programmers not to consider the CPU specifications. The
class library and CLR make the .NET framework.
1.2 Programming features of C#
It is simple, advanced, object oriented language
It contains data types and classes common for all the .NET languages
The Common Language Runtime (CLR) is similar to the Java Virtual Machine
(JVM)
C# provides support for encapsulation, inheritance, polymorphism, and
interfaces
Visual Studio provides support to VC++, Visual Basic, Vbscript, and Jscript
.NET consists of class library and common execution engine
Garbage collection, automatic memory management, interoperability are inbuilt
in C#
User can develop console, windows and web applications using C#
1.3 C# Environment
C# is a part of .NET framework. It is used for creating .NET applications. Using .NET
framework, user can design, deploy and develop the applications. Robust applications
can be easily built using the simple programming model.
Components of .NET framework
The core component of the .NET framework is the CLR. It is an environment where the
programs are executed. The code in CLR is translated into Intermediate Language (IL).
This IL code is then used across different platforms.
The IL code is converted into machine language by the Just in Time (JIT) compiler. The
complier checks for the type safety. This ensures objects are accessed in a compatible
way.
.NET framework class library
The class library works with any .NET languages like VB.NET, VC# and VC++.NET.
The library provides classes used in the code for performing different programming
tasks like data collection, string management, file access and connecting to the database.
CLR contains set of common rules used by all the programming languages in .NET
framework. They are known Common Language Specification (CLS). CLS helps an
object to interact with objects or applications of other languages.
Example 1:
using System;
namespace welcome
public class WelcomeUser
{
static void Main( string[] args)
{
Console.WriteLine("WelcomeUser");
Console.Read();
}
}
The class consists of data and method definitions used by the program. The class can
have one or many methods. Every class must have a Main method, which is the first
method run in the code. In the above code, WelcomeUser class is declared.
Class Methods
Methods in a class specify the behavior of the statement. In the above code, WriteLine
method is used for writing the value in the console. It is defined in the Console class
inside the System namespace.
The Read method of the Console class is used for waiting till the user hits a key. Thus
prevents the screen from closing too quickly.
Comments
Comments are text useful for providing additional information about the code. The
compiler ignores any code that is placed inside a comment block. There are two types
of comments; one comment is used for single line entries and the other for multi line
entries.
For example:
Example 2:
using System;
namespace comment
/*It is a simple code
Used for writing value to the console
*/
class Demo
{
static void Main()
{
//It is added inside Main method
Console.WriteLine("Demonstration of code");
}
}
2.2 User Interface elements
There are various user interface elements present in the Visual Studio application that
can be used in a project. We shall explore the user interface elements in detail.
Start Page
The Start Page is the initial page that gets displayed when the user opens the Microsoft
Visual Studio application.
The Visual Studio IDE provides the start page as the default home page. Through the
start page user can specify the preferences, developer communication using the .NET
platform, exploring new .NET features.
In Visual Studio .NET, the Projects tab displays the recent projects and the latest
modification date. User can use any of the existing projects from the list. Click on the
New Project button when you need to work for a new project. Click on the Open project
button when user wants to open the existing project.
Standard Toolbar
The standard toolbar is used to provide the shortcut menu commands. There are several
buttons on the toolbar that help user to perform tasks related to opening, closing, saving,
editing, pasting on the file.
There are functions related to the tools present in the standard toolbar. They are as
listed below.
New Project: A new project can be created in the application. The button is
used.
Add New Item: A new item is added to the project. The button is used
Save: All the programs created in a particular solution are saved. The button
is used
Save All: It saves all the unsaved items in an application. The button is used
Cut: The selected objects are placed on the clipboard using this option. The
icon is used
Copy: The copy of the selected item is kept on the clipboard. The icon is
used
Paste: It is used to paste the contents in the document. The icon is used
Debugging: The compilation and execution of the project is done. The icon is
used.
Solution Explorer
In the solution explorer window, classes, project and solution name used in the project
gets displayed. Double click the file in the solution explorer for opening the file.
Output window
The messages for the status of the features of Visual Studio .NET IDE are provided by
the output window. The current status of the application is displayed when the user
compiles it. The number of errors present during compilation is displayed in the
window. The View -> Output Window option is used to open the window.
The following figure shows the output window in Visual Studio application.
Error List
The list of errors present in the application is displayed in the error list window. The
user can locate errors as soon as the code is compiled. Double click the error and the
source for it is located. Click View, Error List Window option to open the error list
window.
The class view window is useful in displaying classes, properties and methods
associated with a file. The tree view structure is used to display the items. The code
editor window can be viewed by double clicking the item.
The window contains two buttons, one for sorting the items, other for new folder
creation. The View, Class View option is used for opening the class view window.
Code Editor
User can enter or edit code in the code editor. User can add code to the editor for the
class.
The compiling of the code of C# program using the command line of the Visual Studio
IDE is possible.
1) Add the code in the text editor and save the file with .cs extension
2) Open the command prompt and navigate to the file
3) The csc filename.cs and compile the code
4) The command prompt moves to the next line and creates an executable file
5) Add the filename and execute the program
Chapter 3 Syntax, Data Types, and conversion
3.1 Different keywords in C#
Keywords are special predefined reserved words and are each assigned with a unique
meaning. These keywords can be organized in to categories useful for better
understanding. Below is a list of keywords categorized into different types.
1) Keywords for class, method, field and property
abstract
extern
internal
new
const
override
protected
private
public
sealed
readonly
static
virtual
void
2) Keywords for type conversions
explicit
implicit
as
is
operator
sizeof
typeof
3) Keywords useful for program flow control
if
else
for
foreach
in
case
break
continue
return
while
goto
default
do
switch
4) Keywords used for built in types and enumerations
bool
char
class
byte
decimal
enum
double
float
interface
long
int
object
sbyte
short
string
uint
struct
ulong
ushort
5) Keywords used for exception handling
try
catch
throw
finally
checked
unchecked
6) Keywords used as literals, method passing parameters
true
false
null
this
value
out
params
ref
delegate
event
new
stackalloc
unsafe
3.2 Data Types in C#
Data types are used to store the data in a specific type. There are several built in data
types that are used by the programmers for declaring data.
Every data type has a limited set of options it can be, these limited number of options
are called the data range for the data type. Listed below are the different data types
present and the data range that they can be:
bool: Used to represent the Boolean value. The values that can be assigned are true
or false.
byte: 8 – bit unsigned integer. The range of value for a byte data type is from 0 to
255
char: 16 – bit Unicode character. The range of values is from U +0000 to U + ffff
double: 64 – bit double precision floating point type. The range of values is from
(+/-) 5.0 x 10-324 to (+/-) 1.7 x 10308
decimal: 128 bit precise decimal values with significant digits. The range of values
is from ( -7.9 x 1028 to 7.9 x 1028 ) / 100 to 28
float: 32 bit single precision floating point. The range of values is from -3.4 x 1038
to + 3.4 x 1038
int: 32 – bit signed integer type. It has range of values from -2,147,483,648 to
2,147,483,647
sbyte: 8 – bit signed integer type. It has range of values from -128 to 127
short: 16 – bit signed integer type. It has range of values from -32,768 to 32,767
unit: 32 – bit unsigned integer type. It has range of values from 0 to 4,294,967,295
ushort: 16 – bit unsigned integer type. It has range of values from 0 to 65,535
3.3 Type conversion in C#
Type conversion is useful when the programmer needs to convert from one data type to
another. The type conversion is also known as type casting. There are two types of type
casting in C#. They are implicit type conversion and explicit type conversion.
Implicit type conversion
The implicit keyword is used for implicit conversions. They do not need any casting
operator. These conversions include small to large integral type, from derived class to
base class.
The explicit conversions are done explicitly by users through the use of the pre-defined
functions. In these conversions, a cast operator is needed.
Example 3:
using System;
namespace TypeConversion
{
class Conversion
{
static void Main(string[ ] args)
{
double d=10.243;
int i;
i = int (d);
Console.WriteLine(i);
Console.Read();
}
}
}
The following code snippet shows the conversion of value type to Double type.
Example 4:
using System;
namespace type
{
class DoubleConversion
{
int a=10;
float f=30.05f;
bool b=false;
Console.WriteLine(a.ToDouble());
Console.WriteLine(f.ToDouble());
Console.WriteLine(b.ToDouble());
Console.Read();
}
}
Chapter 4 Variables and Constants
4.1 Exploring variables in C#
A variable is name assigned to the memory location used by the programs. Every
variable has a data type associated with it. The data type determines the size of the
variable’s memory used for storing within the memory.
1. Integral types: int, unit, short, byte, sbyte, long, ulong, and char
2. Floating point types: double, float
3. Decimal types: decimal
4. Boolean type: true or false values
5. Nullable type: nullable data type
6. Reference type: class
Defining Variables
<data_type> <variable_list>;
Where, data_type is a valid C# data type. It can be int, char, float, double, or a user
defined type. The variable_list contains one or more identifiers.
Examples of variables
int a,b,c;
char x,y;
float price, totolcost;
double area;
Initializing Variables
The variables are initialized using an equal sign and followed by an expression. The
syntax for initializing variable is:
variable_name = value;
The variables can be initialized in the declaration. The general form for initializing the
variable is:
int a = 4;
char z=’z’;
double y = 12.145;
Example 5:
using System;
namespace VariableDeclare
class Program
{
static void Main(string[] args)
{
int a;
short s;
double d;
/*initializing variables*/
a=20;
s=5;
d=a+s;
Console.WriteLine("a={0},b={1},d={2}",a,s,d);
Console.Read();
}
}
The Console class present in the System namespace provides ReadLine() function.
The function is used for accepting input from the user and stored into variable.
Example:
int no;
no=Convert.ToInt32(Console.ReadLine());
The Convert.ToInt32() function converts the data entered by the user to integer data
type. The function accepts the data in string format.
lvalue: The expression is an lvalue appears on the left side of the assignment.
rvalue: The expression is an rvalue that appears on the right side but not on the
left side of the assignment.
The variables are lvalues and hence appear on the left side of the assignment. The
numeric values are rvalues and hence appear on the right side.
int a=50;
4.2 Constants and literals in C#
Constant is a class member that represents a fixed value. Constant value can be
computed at compile time but cannot be modified. Constants are declared using the
const keyword.
Syntax:
Example of Constant:
Example 6:
using System;
namespace constc
class Program
{
static void Main(string[] args)
{
const int i=3;
int x;
Console.WriteLine("The value for x is");
x=Convert.ToInt(Console.ReadLine());
int mult = x*i;
Console.WriteLine("Value of multiplication is:{0}",mult);
Console.ReadLine();
}
}
Literals
A literal is a source code representation of a value. There are different types of literals
in C#.
Integer Literals
Integer literal is used to write values of type int, long, unit, and ulong. It can be
represented as decimal, octal, or hexadecimal constant. The base or radix is specified
by the prefix. The value 0x or 0X represents the hexadecimal, 0 defines octal and
decimal is used without prefix.
Some of the examples of integer literals are 15, 0321, 0x5b, 30l.
Floating point literal consist of integer part, fraction part, decimal part, and an exponent
part. The floating point literals can be represented in exponent or decimal form.
Character Literals
Character literals represent a single character. They are enclosed in single quotes.
Characters that are preceded with a backslash are called escape characters and some
of these escape characters have special meanings when used in a string to designate a
special function such as creating a tab or a carriage return. Below is a list of some of
these special escape characters:
\’ A single quote
\” Double quote
\\ Backslash
\0 null
\a Alert
\b Backspace
\f Form feed
\t Horizontal tab
\v Vertical tab
\r Carriage return
String Literals
There are two string literals types supported by C#: regular string literals and
verbatim string literals. Regular string literals contain zero or more characters
enclosed in double quotes.
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebookname.com