You are on page 1of 16

C# FUNDAMENTALS

.NET Framework

Page 3

Parallel and Distributed Computing-Spring 2024


Instructor: Saima Jawad 1
.NET Framework

❑ Common Language Runtime (CLR)


• a runtime environment
• similar to Java Virtual Machine (JVM)

❑ Framework Class Library (FCL)


• provide services for applications

Page 4

.NET Framework
❑ MicroSoft Intermediate Language (MSIL)

o CPU independent set of instructions


o All .NET languages compile into MSIL
o Similar to Java Byte Code
o Also abbreviated as IL

Page 5

Parallel and Distributed Computing-Spring 2024


Instructor: Saima Jawad 2
C# Compilation & Execution

C# source
code
MSIL

C#
compiler
CLR

Machine
code

Page 6

MSIL Advantages
o Portability between OS
➢ .NET compliant languages are all compiled into
MSIL (portable between OS)

o Language Interoperability
➢ Different languages can communicate easily
➢ MSIL code from different languages can be
linked together to form a program

Page 7

Parallel and Distributed Computing-Spring 2024


Instructor: Saima Jawad 3
Interoperability
C#.NET VB.NET ASP.NET
Compile into
MSIL

Link the MSIL codes


MSIL MSIL MSIL

CLR generate single


application (native
Windows code)
Native Code

Page 8

C# Data Types
Page 9

Parallel and Distributed Computing-Spring 2024


Instructor: Saima Jawad 4
C# Data Types
There are 15 data types in C#
Eight to represent integers:
◦ byte, sbyte, short, ushort, int, uint, long, ulong

Two to represent floating point numbers


◦ float, double

One to represent decimals: decimal


One to represent boolean values: bool
One to represent characters: char
One to represent strings: string
One to represent objects: object

Page 10

Numeric Data Types


Type Storage Range
byte 8 bits 0 - 255 System.Byte
sbyte 8 bits -128 - 127 System.SByte
short 16 bits -32,768 - 32767 System.Int16
ushort 16 bits 0 - 65537 System.UInt16
int 32 bits -2,147,483,648 – 2,147,483,647
uint 32 bits 0 – 4,294,967,295
System.Int32
long 64 bits -91018 to 91018
System.UInt32
ulong 64 bits 0 – 1.81019
System.Int64
System.UInt64
decimal 128 bits 1.010-28; 7.91028 with 28-29 significant digits
System.Decimal
float 32 bits 1.510-45; 3.41038 with 7 significant digits
System.Single
double 64 bits 5.010-324; 1.710308 with 15-16 significant digits
System.Double

Page 11

Parallel and Distributed Computing-Spring 2024


Instructor: Saima Jawad 5
Operators’ Precedence and Associativity
Operators Associativity Type
high () left to right parentheses
right to left unary postfix
++ --
++ -- + - right to left unary prefix
* / % left to right multiplicative
+ - left to right additive
< <= > >= left to right relational
== != left to right equality
?: right to left conditional
low = += -= *= /= %= right to left assignment

Page 12

A Simple C# Program

// This program prints a string called "Hello, World!”

using System;

class HelloWorld
{
static void Main(string[] args)
{
Console.WriteLine(“Hello, World!”);
}
}

Page 13

Parallel and Distributed Computing-Spring 2024


Instructor: Saima Jawad 6
C# Keywords
abstract as base bool break
byte case catch char checked
class const continue decimal default
delegate do double else enum
event explicit extern false finally
fixed float for foreach get
goto if implicit in int
interface internal is lock long
namespace new null object operator
out override params private protected
public readonly ref return sbyte
sealed set short sizeof stackalloc
static string struct switch this
throw true try typeof uint
ulong unchecked unsafe ushort using
value virtual void volatile while

Page 14

C# Console Application
using System;
class Hello
{
static void Main(string[] args)
{
Console.Write(“Your Name Please: “);
string name = Console.ReadLine();
Console.WriteLine(“Hello “ + name);
}
}

Page 15

Parallel and Distributed Computing-Spring 2024


Instructor: Saima Jawad 7
C# Windows Application
// Printing multiple lines in a dialog Box.
using System;
using System.Windows.Forms;
class Welcome
{
static void Main( string[] args )
{
MessageBox.Show("Welcome\n to\n C#\n programming!");
}
}

Page 16

Example: Computing Class Average


static void Main( string[] args )
{
int total, // sum of grades
gradeCounter, // number of grades entered
gradeValue, // grade value
average; // average of all grades

// initialization phase
total = 0; // clear total
gradeCounter = 1; // prepare to loop
// processing phase
while ( gradeCounter <= 10 ) // loop 10 times
{
// prompt for input and read grade from user
Console.Write( "Enter integer grade: " );

// read input and convert to integer


gradeValue = Int32.Parse( Console.ReadLine() );
// add gradeValue to total
total = total + gradeValue;

// add 1 to gradeCounter
gradeCounter = gradeCounter + 1;
}

Page 17

Parallel and Distributed Computing-Spring 2024


Instructor: Saima Jawad 8
Example: Computing Class Average
// termination phase
average = total / 10; // integer division
// display average of exam grades
Console.WriteLine( "\nClass average is {0}", average );
} // end Main

Enter integer grade: 100


Enter integer grade: 88
Enter integer grade: 93
Enter integer grade: 55
Enter integer grade: 68
Enter integer grade: 77
Enter integer grade: 83
Enter integer grade: 95
Enter integer grade: 73
Enter integer grade: 62

Class average is 79

Page 18

foreach Loop Example


int sum = 0;
int[ ] grades = {81, 69, 93, 100, 90, 82, 70};

foreach (int g in grades)


{
sum = sum + g;
}
Console.WriteLine(“Sum = {0}”, sum);

Page 19

Parallel and Distributed Computing-Spring 2024


Instructor: Saima Jawad 9
The Array Class
The Array class, defined in the System namespace, is the base
class for arrays in C#.
The Array class provides methods for creating, manipulating,
searching, and sorting arrays.

System.Array

Page 20

Array: Length
Each array has a public property called Length that stores the
size of the array
◦ once an array is created, it has a fixed size

It is referenced using the array name:


while(i <= grades.Length)

Note that Length holds the number of elements, not the


largest index

Page 21

Parallel and Distributed Computing-Spring 2024


Instructor: Saima Jawad 10
Example: Sum of Array Elements

static void Main()


{
int[] num = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int total = 0;
for ( int i = 0; i < num.Length; i++ )
total += num[ i ];
MessageBox.Show( "Total of array elements: " + total,
"Sum the elements of an array",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
} // end Main

Page 22

The Array Class: Methods


BinarySearch This method searches a one-dimensional sorted Array
for a value, using a binary search algorithm.
Clear This method removes all items of an array

Copy This method copies a section of one Array to another


Array

Reverse This method reverses the order of the items in a one-


dimensional Array or in a portion of the Array.

Sort This method sorts the items in one-dimensional Array


objects.
GetLength This method returns the number of items in an Array.

Page 23

Parallel and Distributed Computing-Spring 2024


Instructor: Saima Jawad 11
Sorting the Array
static void Main ()
{
int[] numbers = { 4, 3, 8, 0, 5,-2, 9 };
Array.Sort(numbers);
foreach (int i in numbers)
Console.WriteLine(i);
Console.ReadLine();
}

24

Page 24

Jagged Arrays
A Jagged array is often called an array of arrays.
An element of a jagged array itself is an array.

25

Page 25

Parallel and Distributed Computing-Spring 2024


Instructor: Saima Jawad 12
Jagged Arrays in Memory
float[][] jA = new float [6][];
jA[0] = new float [5] { 0.0, 0.1, 0.2, 0.3, 0.4};
jA[1] = new float [3] {1.0, 1.1, 1.2};
……..

jA jA[0]

jA[1]

jA[2]

jA[3]

jA[4]

jA[5]

Page 26

Jagged Array Example


int[][] arr = new int [2][];
arr[0] = new int [5] { 1, 3, 5, 7, 9 }; Output
arr[1] = new int [4] { 2, 4, 6, 8 };
Array 0: 1 3 5 7 9
for (int i = 0; i < arr.Length; i++)
Array 1: 2 4 6 8
{
Console.Write(“Array {0}: ", i);
for (int j = 0; j < arr[i].Length; j++)
{
Console.Write("{0} ", arr[i][j]);
}
Console.WriteLine();
}

Page 27

Parallel and Distributed Computing-Spring 2024


Instructor: Saima Jawad 13
Properties
A property is much like a combination of a variable and a method
A property consists of 2 parts, a get and a set method, wrapped
inside the property:

class Drawing
{
private string color;
public string Color
{ get { return color; }
set { color = value; }
}
……
}

Page 28
28

Properties
Only one method is required - either get or set, the other is
optional. This allows you to define:
◦ read-only and write-only properties.

public string Color


{ set
{
if (value == "Red") color = value;
else
Console.WriteLine("This car can only be red!");
}
}

Page 29
29

Parallel and Distributed Computing-Spring 2024


Instructor: Saima Jawad 14
Properties Example
public class Date
{
private int month;
public int Month
{
get
{ return month; }

set
{
if ( (value > 0) && (value < 13) )
{
month = value;
}
}
}
}

Page 30

Let’s Program a Frequency Histogram ☺

Page 31

Parallel and Distributed Computing-Spring 2024


Instructor: Saima Jawad 15
static void Main( string[] args )
{
int[] n = { 19, 3, 15, 7, 11, 9, 13, 5, 17, 1 };
string output = "";
output += "Element\tvalue\tHistogram\n“ ;
// build output
for ( int i = 0; i < n.Length; i++ ) {
output += "\n" + i + "\t" + n[ i ] + "\t“ ;
for ( int j = 1; j <= n[ i ]; j++ ) // print a bar
output += (j % 5 == 0) ? “|” : "*";
}
Console.WriteLine("Histogram \n {0}", output);
} // end Main

Parallel and Distributed Computing-Spring 2024


Instructor: Saima Jawad 16

You might also like