All C#5

Download as pdf or txt
Download as pdf or txt
You are on page 1of 33

Table of Contents

1 Introduc on@Tt_001................................................................................ 2
1.1 Programming Paradigms : .......................................................... 2
1.2 Pre Dot Net: ............................................................................. 2
1.3 With Dot Net 2002 : .................................................................. 2
1.4 Net Code Compila on Steps ...................................................... 3
2 Chapther1 ............................................................................................................. 4
2.1 Comments_Regions .................................................................. 4
2.2 Errors ...................................................................................... 4
2.3 Naming_Convension ................................................................. 4
2.4 Variables ................................................................................. 5
2.5 DataType ................................................................................. 5
Value Type Reference Type ................................................. 5
2.6 DataTypes................................................................................ 6
2.6.1 ValueType .................................................................................................. 6
2.6.2 Reference Type.................................................................................... 6
2.7 Object ..................................................................................... 7
#references type (+) 7
2.8 Frac ons and Discard ................................................................ 8
2.9 Value Type Cas ng .................................................................... 8
2.9.1 Implicit cas ng..................................................................................... 8
2.9.2 Explicit cas ng...................................................................................... 8
2.9.3 parse (method) ................................................................................... 9
2.9.4 convert(class) ........................................................................................ 9
3 Chapter2 .................................................................................................................10
3.1 Operators: ..............................................................................10
3.1.1 Operators priority ...........................................................................12
3.2 Block Scope ............................................................................12
3.3 String format...........................................................................13
3.4 Control Statements ..................................................................13
3.4.1 Condi onal Statements ...........................................................13
3.4.2 Loop Statements ..............................................................................15
3.5 String .....................................................................................16
3.6 StringBuilder ...........................................................................17

0
C#
3.7 Deep copy && shallow copy......................................................18
3.7.1 DeepCopy .................................................................................................18
3.7.2 shallowCopy ..........................................................................................18
4 Chapter 3 ................................................................................................................19
4.1 Array ......................................................................................19
4.1.1 One D Array ............................................................................................19
4.1.2 Two 2D Array ........................................................................................20
4.1.3 Array Methods ....................................................................................22
4.2 Boxing / UnBoxing ...................................................................23
4.2.1 Boxing ..........................................................................................................23
4.2.2 UnBoxing...................................................................................................23
4.3 Nullable Types .........................................................................24

|Page1
C#
1 Introduc on@Tt_001
1.1 Programming Paradigms :
 Linear/Sequen al
 Structure
 OOP

C  Structure / Func onal Programming


C++  Not Pure OOP
Java  Pure OOP
COOL  C like Object Oriented Language [Pure OOP]
C++++  C++++  C#

1.2 Pre Dot Net:


Native Code
C++  Compiler  Platform dependent
Machine Language
001010101010101010
1010100110101010101
0
1.3 With Dot Net 2002 :
Dot Net Framework: Is a big container for so ware technologies like
Web (ASP) , Mobile (Xamarin) , Desktop (WPF).
Consis ng of 2 Components
1. Base Class Library(BCL) of Technology
2. Common Language Run me (CLR)

Dot Net Programming Languages LanguageCompilers RunTime


1. Visual Basic Roslyn Components
2. Visual C++

3. C#

4. J# (Disconnected)

5. F#

|Page2
C#
1.4 Net Code Compila on Steps
JIT compiler

CLR
Overhead at Runtime : Jitting
Common language runtime (CLR or just runtime) 1. Jit Compiler (64 Bit) so it is very fast
2. Jitting Happen per Function call
is an environment that runs the code and
3. Jitting For first call only as long as
provides services that make the development process easier.program is not terminated

|Page3
C#
2 Chapther1
2.1 Comments_Regions
1): //  Single Line comment

2):/* syntax */  mul Line comment

3): ShortCut  Ctrl K+C (comment)  Ctrl K+U (uncomment)

4):#region comment // byShortCut  Ctrl K+S

/* syntax */  Read by compiler

#end region

2.2 Errors
1): syntax error  iint x = 5; so error

2): run me Error  The error occurs at run me

3): logical Error  forgot to handle a tea bag in the code

4): Warning  not error but this code not used or there ⚠

___________________________________________________________________________

2.3 Naming_Convension
firstName  Camal Case
FirstName  Pascal Case  used in C#
First-Name  Kabab Case  Angular
___________________________________________________________________________

|Page4
C#
2.4 Variables
Steps: Data Type  Name  Value  Address
EX:_ int x = 5; Address  CLR Managed code
DataType + Name + Value  CLR Reseve (Address) at memory

_________________________________________
2.5 DataType
Common Type System (CTS)
VB
CLS  common language Specifica on vh
F# C#

CLS

Value Type Reference Type


(int) Know size Variable Stack ( String) Unknown Size Data  Heap
Prema ve Types UnPrema ve Types

|Page5
C#
2.6 DataTypes
2.6.1 ValueType

OutPut x=4 , y=5


#---------------------------------------------------------#
2.6.2 Reference Type

Output  = 5

|Page6
C#
2.7 Object

ToString  The class is presented as a String


GetHashCode()  display address located in memory
Equals(object)  Compare
GetType()  Ge ng the Type in The Class


#-------------------------------------#
#references type (+) :_
Where  p1 point & p2 point = two address

Where  p1 = p2 = one address

|Page7
C#
2.8 Frac ons and Discard

Frac ons: readonly struct system 

Solu on 
#------------------------------------------------#

Discard: using to readability ( _ )no read

2.9 Value Type Cas ng


2.9.1 Implicit cas ng
 It can convert from small data to larger data

2.9.2 Explicit cas ng


 Convert from large data to smaller data depending on the data

But large data > small data will happen overflow 


Check  Be sure is useful or not, if it is not possible,out excep on // uncheck  no check this code

|Page8
C#
2.9.3 parse (method)
 convert String to any type ///Because console back string

Console.write  to write on the same line


Console.writeLine to write under line
Consol.clear();  to clear consol and display data

#---------------------------------------------------#
2.9.4 convert(class)
 any Data Type to Data Type

#Can convert in the mode that allows; !

NoAllow
& If possible, it will be converted

|Page9
C#
3 Chapter2
3.1 Operators:

Unary Binary Assignm Rela on Logical Bitwise Ternary


ent al
++ + = == ! | =?:

-- - += != && &

* -= >= || ^

/ *= <= <<

% /= > >>

%= <

| P a g e 10
C#

&&  And ||  or !  Not

#longCircuitCheck all opera ons, even if it false.

#shortCircuit Check but there is false stop.


Be er to use this for opera on Logical Operators
----------------------------------------------------------------------------------------
This way with If Condi on ; this by Ternary operator;

Message = x > y ?  Is the x greater than y? true  print“ “


:  else () print” “

| P a g e 11
C#
3.1.1 Operators priority

Example:

3.2 Block Scope


Not work because variable in scope not public

Work because variable public

| P a g e 12
C#
3.3 String format
Step1: but Immutable Type Can`t change A er crea on

Step2  Composite Forma ng

Step3 String Interpoli on $

3.4 Control Statements


3.4.1 Condi onal Statements
IF / switch
By IF:

| P a g e 13
C#
By switch:

Goto  using by switch


Using to no duplica on data

#Note:
 In case of a condi on, it is be er to use an IF condi on
#------------------------------------------------------------------#
 But if it exists Equality using switch because make Jump table
#jump table :_ Standing on the step is required

| P a g e 14
C#
3.4.2 Loop Statements

)> For ,while , do-while ,foreach <(


Known number of rolls used:_ { For , Foreach }
Unknown  { while , do-while}
#-------------------------------------------------------------3
1) : for 2): foreach

Different between for / foreach ( for can revise in array /but for each can`t)
# but Same performance, same speed !

3): do-while

# flage  true or false /// why: !flag because out false while out but true  new return

| P a g e 15
C#
3.5 String
# String name ;
CLR  Allocate 4 byte at Stack for Reference
CLR  Allocate 0 byte at Heap
# Name = “Tharwat”; // Syntex Sugar
CLR Allocate required at butes at heap 2*7 byte
CLR  Reference name Allocate object at heap (Address)

String  Unmutable Type…


--\> When changing or upda ng the string, the address changes </--

| P a g e 16
C#
3.6 StringBuilder

Stringbuilder  Mutable Type…


--\>When changing or upda ng the string, it will be at the same address </--
End Using Linked List(data Structure)
#----------------------------------------------------------------------#

Append  built in func on using to adding


EX:_ Massage.Append(“route”)
#----------------------------------------------------------------------#

| P a g e 17
C#
3.7 Deep copy && shallow copy
3.7.1 DeepCopy

3.7.2 shallowCopy

#---------------------------------------------------------------#

Clone  Copy the data and add it to the new view with a new address for the data

| P a g e 18
C#
4 Chapter 3
4.1 Array
4.1.1 One D Array
There 4 Ways to Write Array

1) This way is bad 📛

2) way

3) way

4) this way is the best

#-------------------------------------------------------#

Length  size of the Array


Rank  the number of Dimensions
| P a g e 19
C#
4.1.2 Two 2D Array
1 ): Rectangular Array // It has the number of rows equal to the number of columns;

#Display Array  using 2 for Loob:

#---------------------------------------------------------#
#Dispaly Array  using 1 for Loob:

# the explana on:

| P a g e 20
C#
2): Jagged Array // number of rows not Equal number of columns;

#--> Jagged Array is one Dimension Array of other Array with Different Sizes;

#-----------------------------------------------------------------------------#

| P a g e 21
C#
4.1.3 Array Methods
#->1<-# Class Mumber Methods

$-->{ sort , Copy , ConstrainedCopy, clear , CreateInstance , Index ,…..}$


#----------------------------Sort---------------------------#

#---------------------------Copy----------------------------#

#----------------------ConstrainedCopy----------------------#

#------------------------Create_Instance-----------------------------#

Create a new Array:_


++> you can using 2D Array  Rectangular / jagged  <++

| P a g e 22
C#
#->2<-#Object Mumber Methods [Non Sta c Methods]
$-->{ Copyto , Element at , where , append , any , Getlength, Set value}$

#----------------------------------------------------------------------------------------#
4.2 Boxing / UnBoxing
4.2.1 Boxing
 Cas ng Value Type  Reference Type

4.2.2 UnBoxing
 Cas ng From reference Type to Value Type

| P a g e 23
C#
4.3 Nullable Types

//-> Unsafe Cas ng --/-- Implicit Cas ng -<\\

Syntax suger 

More sugar

| P a g e 24
C#
4.4 Null Propaga on

##--> When I give Length no value  handling null <--##

Int?  nullable Type

arr?  nullable Propaga on

| P a g e 25
C#
5 Chapter 4
5.1 Introduc on to Func on
5.1.1 What you can write in NameSpace
1)  Class
2)  Struct
3)  Enum
4)  Interface
5.1.2 What you can write in Class
1)  Func ons (Methods)
2)  Date (A ributes)
//Main  Is Entry Point

//Class Member Method  Class.Method  Sta c


//Object Member Method  Objet.method  NonSta c
#EX:_

#  Class on the same program, not necessary to write the name of program.
#  in print can`t use \ but can  write  @“ant\thing” or write \\
#  \n  Then a new line
#  \t  Then a space between two sentences

| P a g e 26
C#
5.2 Paramaters
5.2.1 Passing by Value

| P a g e 27
C#
5.2.2 Passing by Reference


#  When we make a referance here, it doesn't make a copy, it
doesn't take a Address on each other #

| P a g e 28
C#
5.3 Reference Type parameter
5.3.1 Passing by Value
This is not protec ve Code

This is protec ve Code

| P a g e 29
C#
5.3.2 Passing by reference

5.3.3 Different between pass by value ,ref:


Pass by value + new Pass by Reference +new

| P a g e 30
C#
5.4 Passing By Out Parameter
Before using OutPut parameter :_
can’t using two return solving the problem

A er Using OutPut parameter :_

///@--> we can using Reference :_

| P a g e 31
C#
5.5 Params

Main

## {Only work with Array}

## {It must be wri en in the last parameter}

## {It can only be wri en once}

##=The benefit of them is that they collect numbers in Array=##

@@@@@@@@@ #--------------------------------------------------------------------# @@@@@@@@@@@

5.6 Excep ons


#--A) System Excep ons
#--1) Invalid Operat
#--2) Format Excep on
#--3) Index out of Range Excep on
#--4) Null reference
#--5) Arithme c Ex
#--B) Applica on Excep ons

| P a g e 32

You might also like