SlideShare uma empresa Scribd logo
1 de 28
Baixar para ler offline
Visual C# .Net using
framework 4.5
Eng. Mahmoud Ouf
Lecture 02
mmouf@2017
Common Type System (CTS)
The CLR include Common Type System (CTS) that defines a set of
built in data type that you can use.
Data Type
Value Type Reference Type
Built-in User-define
mmouf@2017
Common Type System (CTS) (cont)
Value Type
The value type variables directly contain their data.
Reference Type
The reference type variable contains reference to their data. The data is
stored in an object. It is possible for 2 reference type variables to
reference the same object.
Note:
All data types are defined in System namespace.
All data types are derived from System.Object
Value types are derived from System.ValueType.
System.Object
System.ValueType
All value types directly contains data, and they can’t be null
mmouf@2017
Common Type System (CTS) “Built-in”
Category Class name (type
in runtime)
C# data
type
Range DescriptionIntegers
Byte byte 0 to 255 8 bit unsigned integer(1byte)
sByte sbyte -128 to 127 8 bit signed integer(1byte)
Int16 short -32,768 to 32,767 16 bit signed integer(2bytes)
Int32 int -2,147,483,648 to 2,147,483,647 32 bit signed integer(4bytes)
Int64 long 64 bit signed integer(8bytes)
UInt16 ushort 0 to 65,535 16 bit unsigned integer(2bytes)
UInt32 uint 0 to 4,294,967,295 32 bit unsigned integer(4bytes)
UInt64 ulong 64 bit unsigned integer(8byte)
Floating
Points
Single float 1.5 * 10-45 to 3.4*1038 A single-precision (32-bit) floating-point number.
Double double 5.0*10-324 to 1.7*10308 A double-precision (64-bit) floating-point number.
Boolean bool true/false
Char char A Unicode (16-bit) character
mmouf@2017
Declaring a variable
Declaring local variable:
Data_type var_name;
In C#, you can’t use uninitialized variable.
You can assign value to variable when declaring it.
Operator:
Arithmatic operator : + - * / %
Relational operator : < > <= >= == != is
Conditional operator : && || ?:
Increment/Decrement : ++ --
Assignment operator : = *= /= += -=
Logical operator : & | !
mmouf@2017
Converting Data Types
Implicit :
We can convert implicitly within the same type for example (int to
long). It couldn’t fail, but, may loose precision, but not magnitude.
Ex: using System;
class Test
{
public static void Main()
{
int intValue = 123;
long longValue = intValue;
Console.WriteLine(“(long){0} = {1}”, intValue, longValue);
}
}
mmouf@2017
Converting Data Types
Explicitly :
We can convert variables explicitly by using the cast expression. Ex:
class Test
{
public static void Main()
{
long longValue = Int64.MaxValue;
int intValue = (int) longValue;
Console.WriteLine(“(int){0} = {1}”, longValue, intValue);
}
}
mmouf@2017
Creating User-Defined Data Types:
Enumeration
Enumerators are useful when a variable can only have a specific set of
values.
Defining : enum Color {Red, Green, Blue}
Using : Color ColorPalette = Color.Red; OR
Color ColorPalette = (Color) 0;
Console.WriteLine(“{0}”, ColorPalette);
Note :
The enumeration element are of type int and the first element has a
value of 0, each successive element increase by 1. You can change the
behavior as follow:
enum Color {Red = 102, Green, Blue}; //Green = 103, Blue = 104
enum Color {Red = 102, Green = 10, Blue = 97};
mmouf@2017
Creating User-Defined Data Types:
Structure
Defining :
struct Employee
{
public string firstName;
public int age;
}
Using:
Employee CompanyEmployee;
CompanyEmployee.firstName = “Aly”;
mmouf@2017
Control Statement:
A) Selection Statements
1) The if statement
if (Boolean-expression)
{
statement to be executed;
}
else
{
statement to be executed;
}
the if statement evaluates a boolean expression true or false. It is unlike
C, NO implicit conversion from int to bool. Ex:
if(number % 2 == 0)
{ Console.WriteLine(“Even”); }
mmouf@2017
Control Statement:
A) Selection Statements
1) The if statement
But, if(number % 2) { } //error CS0029: Can’t implicitly convert type
‘int’ to ‘bool’.
If(number % 2 == 0)
{
Console.WriteLine(“Even”);
}
else
{
Console.WriteLine(“Odd”);
}
mmouf@2017
Control Statement:
A) Selection Statements
2) The switch Statement
Use the switch statements for multiple case blocks. Use break statement
to ensure that no fall-through occurs the fall-through was C/C++
which is leaving the case without break in order to execute the
following case also. This is not supported in C#.
This is sometimes a powerful feature, more often it is the cause of hard
to find bug. The switch statement has the following:
1. test for equality
2. case label are constant
3. only one case constant
4. switch block can contain local variable
5. optional case : default
mmouf@2017
Control Statement:
A) Selection Statements
2) The switch Statement
The constant label must be: any integer type, char, enum or string
switch in C# can test for string
case null is permitted
The default must have a break
Note :
C# statements associated with one or more case labels can’t silently fall-
through or continue to the next case label. So, use the break statement.
You can group several constants together, repeat the keyword case for
each constant (without break).
mmouf@2017
Control Statement:
A) Selection Statements
2) The switch Statement
Example:
class Selections
{
public static int Main()
{
Console.WriteLine(“Welcome to world of .Net”);
Console.WriteLine(“1 = C# n2 = Managed C++ n3 = VB.Netn”);
Console.WriteLine(“Please Select your implementation Language : ”);
string s = Console.ReadLine();
int n = int.Parse(s);
switch(n)
{
case 1:
Console.WriteLine(“Excellent choice”);
break; mmouf@2017
Control Statement:
A) Selection Statements
2) The switch Statement
case 2:
Console.WriteLine(“Very Good choice”);
break;
case 3:
Console.WriteLine(“Not So Good choice”);
break;
default:
Console.WriteLine(“No choice at all”);
break;
}//end switch
}//end main
}//end class
mmouf@2017
Control Statement:
B) Iteration Statement
1) The for loop statement
It has the following syntax:
for(initializer ; condition ; update)
{
Statement to be repeated;
}
•You can declare more than one variable in the initialization of for loop
But they must be of the same type.
•Also, you can declare more than one expression in the update statement
•The condition statement must be boolean
mmouf@2017
Control Statement:
B) Iteration Statement
2) The while statement
It repeatedly executes an embedded statement while a Boolean
expression is true
It has the following syntax
while(condition)
{
Statement to be repeated;
}
mmouf@2017
Control Statement:
B) Iteration Statement
3) The do..while statement
It is like the while, but it execute then check (i.e. the statement is
evaluated at least once)
It has the following syntax:
do
{
statement to be repeated;
}while(condition);
mmouf@2017
Methods:
A method is a group of C # statements that brought together to make a
specific tasks.
Each method has: name , parameter list, return type and the method
body
Definition and Implementation must be in the class.
public void Message()
{
Console.WriteLine(“Welcome”);
return; //could be written even the function return void
Console.WriteLine(“Hello”);
}
Using the return statement like this is not useful. If you have enabled the
C# compiler warnings at level 2 or higher, the compiler will display the
following message “Unreachable code detected”
mmouf@2017
Methods:
Each method has its own set of local variable.
You can use them only inside the method.
Memory for local variables is allocated each time the method is called
and released when the method terminates.
You can’t use uninitialized local variable
mmouf@2017
Methods:
Shared variable (static variable) class member.
This is a variable or method at the level of the class
It is used without creating object of the class.
There is only one instance of it for all object created.
It is accessed with the class name.
class CountCall
{
static int nCount;
static void Init()
{
nCount = 0;
}
mmouf@2017
Methods:
static void Call()
{
++nCount;
Console.WriteLine(“Called{0}”, nCount);
}
public static void Main()
{
Init();
Call();
Call();
}
}
mmouf@2017
Methods:
Passing value type data to method.
Parameters allow information to be passed into and out of a method. It
has the form of:
static ret_type method_name (data_type var,…)
{ }
Passing parameters may be of the following ways
•in :parameter value is copied, variable can be changed inside the
method but it has no effect on value outside the method(pass by value)
•in/out :A reference parameter is a reference to a memory location.
It doesn’t create a new storage instead, it refer to the same location(pass
by reference).
Change made in the method affect the caller
mmouf@2017
Methods:
Passing value type data to method.
Assign parameter value before calling the method
static void OneRefOneVal(ref int nId, long longvar)
{
}
When calling the method
int x;
long y
OneRefOneVal(ref x, y);
•out :An output parameter is a reference to a storage location supplied
by the caller. The variable that is supplied for the out parameter doesn’t
need to be assigned a value before the call is made.
The out variable MUST be assigned a value inside the method.
mmouf@2017
Methods:
Passing value type data to method.
Note:
You can just out data in the out variable, but you can’t make any
processing on it, even if the out variable has a value before passing it.
You can use this variable after assigning it a value
static void OutDemo(out int p)
{
p = 5;
}
int n;
OutDemo(out n); //n will have the value 5
mmouf@2017
Methods:
Using Variable-length Parameter Lists
C# provides a mechanism for passing variable-length parameter list.
You can use the params keyword to specify a variable length parameter
list. To declare a variable length parameter you must:
1. Declare only one params parameter per method
2. place the params at the end of parameter list
3. declare the params as a single dimension array, that’s why all value
must be of the same type
It is useful to send an unknown number of parameters to a function.
mmouf@2017
Methods:
Using Variable-length Parameter Lists
long AddList(params long[] v)
{
long total = 0;
for(int I = 0 ; I < v.length ; I++)
total += v[I];
return total;
}
Calling the function:
long x;
x = AddList(63, 21, 84);
you can send another parameter to the function, but it must be before the
params
long AddList(int a, params long[] v){…}
call : AddList(2, 3, 4,5);
mmouf@2017
Methods:
Methods Overloading
It is possible for two methods in a class to share the same name, but they
must differ in parameter list (number or data type)
Class OverloadingExample
{
static int Add(int a, int b)
{
return (a + b);
}
static int Add(int a, int b, int c)
{
return (a + b + c);
}
static void Main()
{
Console.WriteLine(Add(1, 2) + Add(1, 2, 3));
}
} mmouf@2017

Mais conteúdo relacionado

Mais procurados

03 oo with-c-sharp
03 oo with-c-sharp03 oo with-c-sharp
03 oo with-c-sharp
Naved khan
 
Advanced programming topics asma
Advanced programming topics asmaAdvanced programming topics asma
Advanced programming topics asma
AbdullahJana
 

Mais procurados (20)

Intake 38 6
Intake 38 6Intake 38 6
Intake 38 6
 
Intake 38_1
Intake 38_1Intake 38_1
Intake 38_1
 
Intake 37 ef2
Intake 37 ef2Intake 37 ef2
Intake 37 ef2
 
Method overloading and constructor overloading in java
Method overloading and constructor overloading in javaMethod overloading and constructor overloading in java
Method overloading and constructor overloading in java
 
03 oo with-c-sharp
03 oo with-c-sharp03 oo with-c-sharp
03 oo with-c-sharp
 
Differences between method overloading and method overriding
Differences between method overloading and method overridingDifferences between method overloading and method overriding
Differences between method overloading and method overriding
 
Introduction to C++
Introduction to C++Introduction to C++
Introduction to C++
 
Chapter 5:Understanding Variable Scope and Class Construction
Chapter 5:Understanding Variable Scope and Class ConstructionChapter 5:Understanding Variable Scope and Class Construction
Chapter 5:Understanding Variable Scope and Class Construction
 
Intake 37 linq2
Intake 37 linq2Intake 37 linq2
Intake 37 linq2
 
Classes, Objects and Method - Object Oriented Programming with Java
Classes, Objects and Method - Object Oriented Programming with JavaClasses, Objects and Method - Object Oriented Programming with Java
Classes, Objects and Method - Object Oriented Programming with Java
 
Introduction to c#
Introduction to c#Introduction to c#
Introduction to c#
 
2CPP08 - Overloading and Overriding
2CPP08 - Overloading and Overriding2CPP08 - Overloading and Overriding
2CPP08 - Overloading and Overriding
 
Constructors destructors
Constructors destructorsConstructors destructors
Constructors destructors
 
Inheritance
InheritanceInheritance
Inheritance
 
Java Foundations: Methods
Java Foundations: MethodsJava Foundations: Methods
Java Foundations: Methods
 
Method overloading
Method overloadingMethod overloading
Method overloading
 
Advanced programming topics asma
Advanced programming topics asmaAdvanced programming topics asma
Advanced programming topics asma
 
2CPP14 - Abstraction
2CPP14 - Abstraction2CPP14 - Abstraction
2CPP14 - Abstraction
 
Method of java
Method of javaMethod of java
Method of java
 
Intake 37 linq1
Intake 37 linq1Intake 37 linq1
Intake 37 linq1
 

Destaque

DALIA BALČYTYTĖ_englCV
DALIA BALČYTYTĖ_englCVDALIA BALČYTYTĖ_englCV
DALIA BALČYTYTĖ_englCV
Dalia Balcytyte
 

Destaque (14)

PRDC12 advanced design patterns
PRDC12 advanced design patternsPRDC12 advanced design patterns
PRDC12 advanced design patterns
 
Aymandcv
AymandcvAymandcv
Aymandcv
 
Config management
Config managementConfig management
Config management
 
DALIA BALČYTYTĖ_englCV
DALIA BALČYTYTĖ_englCVDALIA BALČYTYTĖ_englCV
DALIA BALČYTYTĖ_englCV
 
Ecosistemul Drupal
Ecosistemul DrupalEcosistemul Drupal
Ecosistemul Drupal
 
Գիտագործնական հավաք կրթահամալիրում
Գիտագործնական հավաք կրթահամալիրումԳիտագործնական հավաք կրթահամալիրում
Գիտագործնական հավաք կրթահամալիրում
 
Drupal Winter Day 2017 Workshop - Personal Blog
Drupal Winter Day 2017 Workshop - Personal BlogDrupal Winter Day 2017 Workshop - Personal Blog
Drupal Winter Day 2017 Workshop - Personal Blog
 
Drupal Winter Day 2017
Drupal Winter Day 2017Drupal Winter Day 2017
Drupal Winter Day 2017
 
The IS 11769 Part 1 Usage of Asbestos Cement Products
The IS 11769 Part 1 Usage of Asbestos Cement ProductsThe IS 11769 Part 1 Usage of Asbestos Cement Products
The IS 11769 Part 1 Usage of Asbestos Cement Products
 
Intake 37 linq3
Intake 37 linq3Intake 37 linq3
Intake 37 linq3
 
Intake 37 8
Intake 37 8Intake 37 8
Intake 37 8
 
CV_PardeepSingh
CV_PardeepSinghCV_PardeepSingh
CV_PardeepSingh
 
Intake 37 ef1
Intake 37 ef1Intake 37 ef1
Intake 37 ef1
 
Bp economy-for-99-percent-160117-pt
Bp economy-for-99-percent-160117-ptBp economy-for-99-percent-160117-pt
Bp economy-for-99-percent-160117-pt
 

Semelhante a Intake 37 2

434090527-C-Cheat-Sheet. pdf C# program
434090527-C-Cheat-Sheet. pdf  C# program434090527-C-Cheat-Sheet. pdf  C# program
434090527-C-Cheat-Sheet. pdf C# program
MAHESHV559910
 
Introduction of C# BY Adarsh Singh
Introduction of C# BY Adarsh SinghIntroduction of C# BY Adarsh Singh
Introduction of C# BY Adarsh Singh
singhadarsh
 

Semelhante a Intake 37 2 (20)

unit 1 (1).pptx
unit 1 (1).pptxunit 1 (1).pptx
unit 1 (1).pptx
 
CSharp Presentation
CSharp PresentationCSharp Presentation
CSharp Presentation
 
Notes(1).pptx
Notes(1).pptxNotes(1).pptx
Notes(1).pptx
 
434090527-C-Cheat-Sheet. pdf C# program
434090527-C-Cheat-Sheet. pdf  C# program434090527-C-Cheat-Sheet. pdf  C# program
434090527-C-Cheat-Sheet. pdf C# program
 
Chapter 2 c#
Chapter 2 c#Chapter 2 c#
Chapter 2 c#
 
C_plus_plus
C_plus_plusC_plus_plus
C_plus_plus
 
Introduction of C# BY Adarsh Singh
Introduction of C# BY Adarsh SinghIntroduction of C# BY Adarsh Singh
Introduction of C# BY Adarsh Singh
 
Introduction to C#
Introduction to C#Introduction to C#
Introduction to C#
 
CSharpCheatSheetV1.pdf
CSharpCheatSheetV1.pdfCSharpCheatSheetV1.pdf
CSharpCheatSheetV1.pdf
 
Introduction to Visual Basic
Introduction to Visual Basic Introduction to Visual Basic
Introduction to Visual Basic
 
C programming session7
C programming  session7C programming  session7
C programming session7
 
C programming session7
C programming  session7C programming  session7
C programming session7
 
C Language (All Concept)
C Language (All Concept)C Language (All Concept)
C Language (All Concept)
 
C# language basics (Visual Studio)
C# language basics (Visual Studio) C# language basics (Visual Studio)
C# language basics (Visual Studio)
 
C# language basics (Visual studio)
C# language basics (Visual studio)C# language basics (Visual studio)
C# language basics (Visual studio)
 
Data Types, Variables, and Constants in C# Programming
Data Types, Variables, and Constants in C# ProgrammingData Types, Variables, and Constants in C# Programming
Data Types, Variables, and Constants in C# Programming
 
Pc module1
Pc module1Pc module1
Pc module1
 
2CPP18 - Modifiers
2CPP18 - Modifiers2CPP18 - Modifiers
2CPP18 - Modifiers
 
C presentation! BATRA COMPUTER CENTRE
C presentation! BATRA  COMPUTER  CENTRE C presentation! BATRA  COMPUTER  CENTRE
C presentation! BATRA COMPUTER CENTRE
 
Chapter3: fundamental programming
Chapter3: fundamental programmingChapter3: fundamental programming
Chapter3: fundamental programming
 

Mais de Mahmoud Ouf (17)

Relation between classes in arabic
Relation between classes in arabicRelation between classes in arabic
Relation between classes in arabic
 
Intake 38 data access 5
Intake 38 data access 5Intake 38 data access 5
Intake 38 data access 5
 
Intake 38 data access 4
Intake 38 data access 4Intake 38 data access 4
Intake 38 data access 4
 
Intake 38 data access 3
Intake 38 data access 3Intake 38 data access 3
Intake 38 data access 3
 
Intake 38 data access 1
Intake 38 data access 1Intake 38 data access 1
Intake 38 data access 1
 
Intake 38 11
Intake 38 11Intake 38 11
Intake 38 11
 
Intake 38 10
Intake 38 10Intake 38 10
Intake 38 10
 
Intake 38 9
Intake 38 9Intake 38 9
Intake 38 9
 
Intake 38 8
Intake 38 8Intake 38 8
Intake 38 8
 
Intake 38 7
Intake 38 7Intake 38 7
Intake 38 7
 
Intake 38 5 1
Intake 38 5 1Intake 38 5 1
Intake 38 5 1
 
Intake 38 5
Intake 38 5Intake 38 5
Intake 38 5
 
Intake 37 DM
Intake 37 DMIntake 37 DM
Intake 37 DM
 
Intake 37 12
Intake 37 12Intake 37 12
Intake 37 12
 
Intake 37 11
Intake 37 11Intake 37 11
Intake 37 11
 
Intake 37 10
Intake 37 10Intake 37 10
Intake 37 10
 
Intake 37 9
Intake 37 9Intake 37 9
Intake 37 9
 

Último

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Último (20)

Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 

Intake 37 2

  • 1. Visual C# .Net using framework 4.5 Eng. Mahmoud Ouf Lecture 02 mmouf@2017
  • 2. Common Type System (CTS) The CLR include Common Type System (CTS) that defines a set of built in data type that you can use. Data Type Value Type Reference Type Built-in User-define mmouf@2017
  • 3. Common Type System (CTS) (cont) Value Type The value type variables directly contain their data. Reference Type The reference type variable contains reference to their data. The data is stored in an object. It is possible for 2 reference type variables to reference the same object. Note: All data types are defined in System namespace. All data types are derived from System.Object Value types are derived from System.ValueType. System.Object System.ValueType All value types directly contains data, and they can’t be null mmouf@2017
  • 4. Common Type System (CTS) “Built-in” Category Class name (type in runtime) C# data type Range DescriptionIntegers Byte byte 0 to 255 8 bit unsigned integer(1byte) sByte sbyte -128 to 127 8 bit signed integer(1byte) Int16 short -32,768 to 32,767 16 bit signed integer(2bytes) Int32 int -2,147,483,648 to 2,147,483,647 32 bit signed integer(4bytes) Int64 long 64 bit signed integer(8bytes) UInt16 ushort 0 to 65,535 16 bit unsigned integer(2bytes) UInt32 uint 0 to 4,294,967,295 32 bit unsigned integer(4bytes) UInt64 ulong 64 bit unsigned integer(8byte) Floating Points Single float 1.5 * 10-45 to 3.4*1038 A single-precision (32-bit) floating-point number. Double double 5.0*10-324 to 1.7*10308 A double-precision (64-bit) floating-point number. Boolean bool true/false Char char A Unicode (16-bit) character mmouf@2017
  • 5. Declaring a variable Declaring local variable: Data_type var_name; In C#, you can’t use uninitialized variable. You can assign value to variable when declaring it. Operator: Arithmatic operator : + - * / % Relational operator : < > <= >= == != is Conditional operator : && || ?: Increment/Decrement : ++ -- Assignment operator : = *= /= += -= Logical operator : & | ! mmouf@2017
  • 6. Converting Data Types Implicit : We can convert implicitly within the same type for example (int to long). It couldn’t fail, but, may loose precision, but not magnitude. Ex: using System; class Test { public static void Main() { int intValue = 123; long longValue = intValue; Console.WriteLine(“(long){0} = {1}”, intValue, longValue); } } mmouf@2017
  • 7. Converting Data Types Explicitly : We can convert variables explicitly by using the cast expression. Ex: class Test { public static void Main() { long longValue = Int64.MaxValue; int intValue = (int) longValue; Console.WriteLine(“(int){0} = {1}”, longValue, intValue); } } mmouf@2017
  • 8. Creating User-Defined Data Types: Enumeration Enumerators are useful when a variable can only have a specific set of values. Defining : enum Color {Red, Green, Blue} Using : Color ColorPalette = Color.Red; OR Color ColorPalette = (Color) 0; Console.WriteLine(“{0}”, ColorPalette); Note : The enumeration element are of type int and the first element has a value of 0, each successive element increase by 1. You can change the behavior as follow: enum Color {Red = 102, Green, Blue}; //Green = 103, Blue = 104 enum Color {Red = 102, Green = 10, Blue = 97}; mmouf@2017
  • 9. Creating User-Defined Data Types: Structure Defining : struct Employee { public string firstName; public int age; } Using: Employee CompanyEmployee; CompanyEmployee.firstName = “Aly”; mmouf@2017
  • 10. Control Statement: A) Selection Statements 1) The if statement if (Boolean-expression) { statement to be executed; } else { statement to be executed; } the if statement evaluates a boolean expression true or false. It is unlike C, NO implicit conversion from int to bool. Ex: if(number % 2 == 0) { Console.WriteLine(“Even”); } mmouf@2017
  • 11. Control Statement: A) Selection Statements 1) The if statement But, if(number % 2) { } //error CS0029: Can’t implicitly convert type ‘int’ to ‘bool’. If(number % 2 == 0) { Console.WriteLine(“Even”); } else { Console.WriteLine(“Odd”); } mmouf@2017
  • 12. Control Statement: A) Selection Statements 2) The switch Statement Use the switch statements for multiple case blocks. Use break statement to ensure that no fall-through occurs the fall-through was C/C++ which is leaving the case without break in order to execute the following case also. This is not supported in C#. This is sometimes a powerful feature, more often it is the cause of hard to find bug. The switch statement has the following: 1. test for equality 2. case label are constant 3. only one case constant 4. switch block can contain local variable 5. optional case : default mmouf@2017
  • 13. Control Statement: A) Selection Statements 2) The switch Statement The constant label must be: any integer type, char, enum or string switch in C# can test for string case null is permitted The default must have a break Note : C# statements associated with one or more case labels can’t silently fall- through or continue to the next case label. So, use the break statement. You can group several constants together, repeat the keyword case for each constant (without break). mmouf@2017
  • 14. Control Statement: A) Selection Statements 2) The switch Statement Example: class Selections { public static int Main() { Console.WriteLine(“Welcome to world of .Net”); Console.WriteLine(“1 = C# n2 = Managed C++ n3 = VB.Netn”); Console.WriteLine(“Please Select your implementation Language : ”); string s = Console.ReadLine(); int n = int.Parse(s); switch(n) { case 1: Console.WriteLine(“Excellent choice”); break; mmouf@2017
  • 15. Control Statement: A) Selection Statements 2) The switch Statement case 2: Console.WriteLine(“Very Good choice”); break; case 3: Console.WriteLine(“Not So Good choice”); break; default: Console.WriteLine(“No choice at all”); break; }//end switch }//end main }//end class mmouf@2017
  • 16. Control Statement: B) Iteration Statement 1) The for loop statement It has the following syntax: for(initializer ; condition ; update) { Statement to be repeated; } •You can declare more than one variable in the initialization of for loop But they must be of the same type. •Also, you can declare more than one expression in the update statement •The condition statement must be boolean mmouf@2017
  • 17. Control Statement: B) Iteration Statement 2) The while statement It repeatedly executes an embedded statement while a Boolean expression is true It has the following syntax while(condition) { Statement to be repeated; } mmouf@2017
  • 18. Control Statement: B) Iteration Statement 3) The do..while statement It is like the while, but it execute then check (i.e. the statement is evaluated at least once) It has the following syntax: do { statement to be repeated; }while(condition); mmouf@2017
  • 19. Methods: A method is a group of C # statements that brought together to make a specific tasks. Each method has: name , parameter list, return type and the method body Definition and Implementation must be in the class. public void Message() { Console.WriteLine(“Welcome”); return; //could be written even the function return void Console.WriteLine(“Hello”); } Using the return statement like this is not useful. If you have enabled the C# compiler warnings at level 2 or higher, the compiler will display the following message “Unreachable code detected” mmouf@2017
  • 20. Methods: Each method has its own set of local variable. You can use them only inside the method. Memory for local variables is allocated each time the method is called and released when the method terminates. You can’t use uninitialized local variable mmouf@2017
  • 21. Methods: Shared variable (static variable) class member. This is a variable or method at the level of the class It is used without creating object of the class. There is only one instance of it for all object created. It is accessed with the class name. class CountCall { static int nCount; static void Init() { nCount = 0; } mmouf@2017
  • 22. Methods: static void Call() { ++nCount; Console.WriteLine(“Called{0}”, nCount); } public static void Main() { Init(); Call(); Call(); } } mmouf@2017
  • 23. Methods: Passing value type data to method. Parameters allow information to be passed into and out of a method. It has the form of: static ret_type method_name (data_type var,…) { } Passing parameters may be of the following ways •in :parameter value is copied, variable can be changed inside the method but it has no effect on value outside the method(pass by value) •in/out :A reference parameter is a reference to a memory location. It doesn’t create a new storage instead, it refer to the same location(pass by reference). Change made in the method affect the caller mmouf@2017
  • 24. Methods: Passing value type data to method. Assign parameter value before calling the method static void OneRefOneVal(ref int nId, long longvar) { } When calling the method int x; long y OneRefOneVal(ref x, y); •out :An output parameter is a reference to a storage location supplied by the caller. The variable that is supplied for the out parameter doesn’t need to be assigned a value before the call is made. The out variable MUST be assigned a value inside the method. mmouf@2017
  • 25. Methods: Passing value type data to method. Note: You can just out data in the out variable, but you can’t make any processing on it, even if the out variable has a value before passing it. You can use this variable after assigning it a value static void OutDemo(out int p) { p = 5; } int n; OutDemo(out n); //n will have the value 5 mmouf@2017
  • 26. Methods: Using Variable-length Parameter Lists C# provides a mechanism for passing variable-length parameter list. You can use the params keyword to specify a variable length parameter list. To declare a variable length parameter you must: 1. Declare only one params parameter per method 2. place the params at the end of parameter list 3. declare the params as a single dimension array, that’s why all value must be of the same type It is useful to send an unknown number of parameters to a function. mmouf@2017
  • 27. Methods: Using Variable-length Parameter Lists long AddList(params long[] v) { long total = 0; for(int I = 0 ; I < v.length ; I++) total += v[I]; return total; } Calling the function: long x; x = AddList(63, 21, 84); you can send another parameter to the function, but it must be before the params long AddList(int a, params long[] v){…} call : AddList(2, 3, 4,5); mmouf@2017
  • 28. Methods: Methods Overloading It is possible for two methods in a class to share the same name, but they must differ in parameter list (number or data type) Class OverloadingExample { static int Add(int a, int b) { return (a + b); } static int Add(int a, int b, int c) { return (a + b + c); } static void Main() { Console.WriteLine(Add(1, 2) + Add(1, 2, 3)); } } mmouf@2017