(C#) Chapter - 4
(C#) Chapter - 4
Exceptions
Error handling is not an afterthought
Type-safety
No uninitialized variables, unsafe casts
Versioning
Pervasive versioning considerations in all aspects of language
design
VARIABLES/IDENTIFIER
is a program element that stores a value. Some of the values might
contain
Number
String
Character
Data
Object
Variable length
Store on the heap
VARIABLE‟S DECLARATION
1. datatype variablename;
int num;
Declaration
string st1;
String st2;
a[0][0]=3; a[1][0]=5;
a[0][1]=4; a[1][1]=6;
a[0][2]=5; a[1][2]=7;
a[1][3]=8;
Case-sensitive
Numeric Literals
Boolean Literals
Character Literals
C#‟s Numeric literal lists
Character Data Type
U unit
L long
UL / LU ulong
F float
D double
M decimal
Example:
int hex = 0x64;
Console.WriteLine("{0:x}",hex);
Console.WriteLine(“Hexa value is“ + hex);
Output:
100
Hexa value is 100
Boolean Literals
2 values
true and false
Example:
bool flag = false;
Console.WriteLine(flag);
Single Character Literals &
String Literals
Example:
char grade=„A‟;
string result=“Credit”;
Backslash Character Literals
\a - Alert (usually the HW beep)
\b - Backspace
\n - New line
\0 - Null
\t - Horizontal tab
\v - Vertical tab
\' - Single quote
\" - Double quote
\\ - Backslash
\f - Form feed
\r - Carriage return
Casting
A cast operator explicitly tells a program
to convert a value from one type to another type
to convert from object to another object
to convert value type to object and then object to value type
vice visa.
Example:
float num = 10.5f;
int number = (int) num;
Console.Writeline(“{0,m}”,number);
Console.Writeline(“number value is” + number);
Console.Writeline(number.ToString());
Output:
10
number value is 10
10
Casting Object to Another Object
Person personA = new Student();
Student student;
student = (Student)personA;
(OR)
• There are three ways you can pass values into a method: by
value, by reference, and for output.
Pass By Value
By default a parameter‟s value is passed into the method by value. That
means the method receives a copy of the parameter‟s value. If the
method modifies the parameter, it modifies only the copy, so the value
in the calling code remains unchanged.
private void DoubleTest( )
{
int value = 10;
DoubleIt(value);
Console.WriteLine("DoubleTest: " + value.ToString());
}
Output:
private void DoubleIt(int number) DoubleIt: 20
{ DoDouble: 10
number *= 2;
Console.WriteLine("DoubleIt: " + number.ToString());
}
Pass By Reference
One alternative to passing a value into a method by value is to
pass it by reference. In that case the method receives a reference
to the argument‟s value, not a copy of the value. That means if
the method changes the parameter, the argument in the calling
code is also changed.
Output:
private void DoubleTest() DoubleIt: 20
{ DoDouble: 20
int value = 10;
DoubleIt(ref value);
Console.WriteLine("DoubleTest: " + value.ToString());
}
General form:
<Accessibility> enum enumeration_name
{enumeration list};
Enumeration
General form
enum Color {Red,Green,Blue};
By default, it starts with 0; can be changed to any number.
enum Color{Red = 10, Green, Blue}
By default, it increments by 1 for each subsequent name.
You can sign a value to each name.
enum Color{ Red = 10, Green = 20, Blue = 21}
You can increment a enum variable.
Color mycolor;
mycolor = Color.Green;
mycolor += 1;
Console.WriteLine(mycolor); // Blue
Integer casting must be explicit
Color background = (Color)2;
int oldColor = (int)background;
Operations on Enumeration
Compare
if (c == Color.red) ...if (c > Color.red && c <=
Color.green) ...
+, -
c = c + 2;
++, --
c++;
&
if ((c & Color.red) == 0) ...
|
c = c | Color.blue;
~
c = ~ Color.red;
Enumeration : Example
using System;
namespace myClass{
class EnumDemo {
enum Apple { Jonathan, GoldenDel, RedDel, Winesap, Cortland,McIntosh};
public static void Main()
{
string[] color = { "Red", "Yellow", "Purple", "Orange", "Greeen","lightgreen" };
Apple i;
for( i = Apple.Jonathan; i <= Apple.McIntosh; i++)
Console.WriteLine(i + " has value of " + i);
for( i = Apple.Jonathan; i <= Apple.McIntosh; i++)
Console.WriteLine(i + " has value of " + (int)i);
for(i = Apple.Jonathan; i <= Apple.McIntosh; i++)
Console.WriteLine("Color of " + i + " is " + color[(int)i]);
Console.ReadLine();
} }}
Enumeration : Example
NULLABLE TYPE
• Most relational databases have a concept of a null data
value. A null value indicates that a field does not contain
any data.
• For example, a null bank balance would indicate that
there is no known balance, whereas a 0 would indicate
that the balance was 0.
• You can create a nullable variable in C# by adding a
question mark after the variable‟s data type.
int? count;
count = null;
count = 1337;
DELEGATES
• A delegate is a reference type that defines a method
signature
• Delegate are object-oriented, type-safe, and secure
function pointers.
• Can invoke both instance and static method.
• All methods invoked by the same delegate must have the
same parameters and return value.
• The method to call is resolved at runtime, not compile
time
• Effective use of Delegate improves the performance of
application.
How to use Delegates?
• Declaration : type declaration
<Accessibility> delegate return_type delegate_name (parameters);
delegate double MathOp( double x, double y);
• Method definition : methods whose references are encapsulated
into a delegate instance are known as delegate methods or callable
entities.
public static double Multiply(double a, double b)
{ return a*b;}
• Instantiation : a delegate-creation-expression is used to create
instance of a delegate
MathOp mop=new MathOp (Multiply)
• Invocation :
double r1 = mop (3.3,4.4);
delegate string StrMod(string str); public static void Main(string[] args) {
class Delegates { string str;
static string ReplaceSpace(string
st) StrMod strOp = new
StrMod(ReplaceSpace);
{
str = strOp("call ReplaceSpcace");
return st;
Console.WriteLine("Result String: " + str);
}
static string RemoveSpace(string strOp = new StrMod(RemoveSpace);
st) str = strOp("call RemoveSpcace");
{ Console.WriteLine("Result String: " + str);
return st; strOp = new StrMod(Reverse);
} str = strOp("call Reverse");
static string Reverse(string st) Console.WriteLine("Result String: " + str);
{ Console.Read();
return st; }
} }
Sample Program
1. Create one dimensional integer array, arr, with the
length of 13. Each index of array, arr, is inserted integer
values. Then, find and display the peak points. Also
display the original array arr.