Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
By
FAISAL NAZEER
Copyright © 2016
ABOUT AUTHOR:
Faisal has more than 4 years of
experience in Programming…He started To learn to code at the age of
14.he is currently making games for mobile platform.
TABLE OF CONTENTS
Introduction
Legal Notes
Chapter 1. (Intro To C#)
Chapter 2. ( Primitive Types and Expressions )
Chapter 3. Non-Primitive Types Chapter 4. Control Flow
About The Author
LEGAL NOTES
Any Part of this book can not be reused or distributed anywhere and
never download this from any unknown source if you find such activity
plz let us know at
Faisalnaziarali@gmail.com
CHAPTER 1.
(INTRO TO C#)
C# ( PRONOUNCED “CSHARP“) IS AN OBJECT- ORIENTED PROGRAMMING
LANGUAGE FROM MICROSOFT THAT AIMS TO COMBINE THE COMPUTING
POWER OF C++ WITH THE PROGRAMMINGEASE OF VISUAL BASIC. C# IS
BASED ON C++ AND CONTAINS FEATURES SIMILAR TO THOSE OF JAVA. C#
IS DESIGNED TO WORK WITH MICROSOFT’S .NET PLATFORM.
WHY C#.
C# IS AN ELEGANT, SIMPLE, TYPE-SAFE, OBJECT-•
Namespaces.
As number of classes grow in our project we need a way to organize related classes so
we use namespace. namespace is a container of same classes
Assembly.
As number of namespaces grow we need a way to partition our application so here
assemblies come handy. Assembly is a container of same namespaces
Physically it is a file on disk it could be (exe or dll).
Our First C Program.
In this book we will use visual studio so open visual studio
On top go to file
>new>project>installed>templates>visual c#>windows>console application
And here we have 3 fields we can give name to our project just name the project
HelloWorld
We can also set the location of the project by clicking browse button
Third we can name solution in visual studio we have solutions one solution can have
more than one project
Finally click ok to create console application
See the screen shoot below
Every console application we create in visual studios has one class called program.cs
In this class we have one method/function and this is the entery point of our
application
static void Main(string[] args)
{
}
This method starts with static which we will later cover in this course
Then return type which is void and void means this function returns
nothing.
And then the Name of the function Main .note that c# is a case sensitive
language So Main has to Start With upper Case M.
Functions can have input and output which we are called arguments this
function takes a string[] array input which we will cover later
And finally we close a code block with curly braces{}.
Now lets write helloworld to the console
Console Application
A console application is a simple application with no graphics . To Display Heloworld
to the console we can use Console Class which is used to write on console and take
input from console.
So inside Main Method Type the code below Console.WriteLine(“HelloWorld”);
Console is a class and WriteLine is the function of Console Class. we can access a
method of class with . (dot) and the name of the function . WriteLine is used to write
a line on console .in () we give the input to that function . we can write anything
inside to print on console in this case we will print helloworld to the console.
To run and see the result press shortcut key to run application in visual studio is
Ctrl + F5
Finished Code :
static void Main(string[] args) {
Console.WriteLine(“HelloWorld”); }
Congrats you have just finished the first chapter.
CHAPTER 2. PRIMITIVE TYPES AND EXPRESSIONS
Now we will dive into some basic c# stuff
VARIABLES AND CONSTANTS :
VARIABLE.
A name given to a storage location in memory
Declaring a variable
Datatype Name of the variable =value we want to assign ;
Every statement in c sharp ends with ;
Example
Int a =10;
Int is datatype , a is name and we assign value 10 with equal sign =
Constant.
An immutable value. This value will not change throughout the lifetime of the
application Example
pi =3.14 which we use to calculate area of circle it should be constant.
Declaring a constant
Const keyword Datatype Name of the constant =value we want to assign ;
Example of pi.
Const float pi=3.14f;
Name of variables and constants also called identifiers and a identifier cannot
start with a number, A identifiers cannot have white spaces, identifier cannot
be reserved keyword which already defined in c language ie(int ,bool ,float).
Always use meaningful names .
NamingConventions .Camel Case: firstName
First Word of camel case starts with lowercase and after all words start with
uppercase
.Pascal Case: FirstName
All letters of all words are uppercase
.Hungarian Notation: strFirstName
First we prefix datatype and thenAll letters of all words are uppercase
Primitive Types in C
To declare character data type we will enter a character inside ‘A’ and for
string we enter string in “string”.
We also use keyword var instead of declaring the data type c compileir
automatically detects the data type. Now change all the data types to var
It should look like this
Format String
Increment operator increases value by one (1) and decrement operator decreases by
one
Comparison Operators
One thing to note here is that equal (=) sign is used to assign value and == is used to
test equality
Logical Operators
These are used in conditional statements which we will discuss later
Bitwise Operators:
These are used in low level programming
Practice operators :
In main method declare two variables and to display their value of sum just use
In this we case declared our variables as int to show division result in floating point
number we have to explicitly tell compliler to treat these variables as flaot by casting
(flaot) to both variables
Example 3
Here we have three variables a,b,c if we want to add a and b first and then multiply
with c we can do it by writing a+b inside (a+b) so now a and b first added then
multiplied with c.
Examppe 4
With and operator we can now check that both conditions are true .here c>b which is
true and c==a which is false so one of the condition is not true then result will be false
Example 6:
We can use or operator if any of these condition is true result will be true
Comments
Comments are used to describe what , why , how , constrains etc….
Comments are not executed
Types of comments
Multiline comments:
Access modifier means who can access it public modifier means anyone can access it
A class can contain fields and methods. In this example method has no parameter or
takes no input or output
Calculator class example
In this example add() function takes input and return a+b output.
Creating Objects
We declare objects like variables but we explicitly have to allocate memory for it
Example of person class which from we now create a object
We can use var keyword to make our code cleaner We can access fields and function
of that object by using the object name and with. Dot and the function and field
name
Person.name=”faisal”; Person.introduce();
Static
We can add static keyword to Add() function of calculator class
When we create objects there are multiple instances of Add method in memory but
static Add function only exists at one place in memory.
Practice Classes:
First create a class in visual studio by pressing
Shit+alt+c
Name that class Person
And now add first name and last name string fields
And a introduce() method which outputs the first and last name on console
See the cod below
Create a another class named Calculator and fill it with Add ()Method
This method takes input of two int variables and return them after adding them
Now come back to main method and create object of calculator class and finally call
Add() function and store that value in a variable to print the result on console.
This function takes two arguments/inputs and add them.
Result should be 3.
Arrays
A data structure to store a collection of variables of the same type
Why use array?
Example:
In this example instead of declaring 3 int variables we can declare one int[] array
Notice the square brackets[] .first brackets tells that it’s a array and 2nd brackets
tells the number of elements in the array. At the time of declaring we need to
initialize it with number of elements.
Accessing the element of a array
To access the first element we start with zero(0) index and second element is index 1
and so on and so forth .we pass the index in square brackets[].and can assign the
value.
In this example
We are concatenating first name and last anme strings together with string string
literal which is empty string and with + sign.
Practice Strings
Open visual studio
To create a string
String keyword name of string =string” Example
String firstname=faisal”
String secondname=nazeer”
//now we can concatenate two strings
Var fullname=firstname + +secondname” Now display it on console
Console.WriteLine(fullname);
CHAPTER 4. CONTROL FLOW
CONDITIONAL STATEMENTS
IF/ELSE STATEMENST