C-Sharp: Presented By: Karen Medhat

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 48

C-Sharp

Presented by: Karen Medhat

C-Sharp
Advanced Object Oriented

.NET platform
C# has roots in C, C++ and Java IDE (Visual Studio)

Examples
using System; class Welcome {
static void Main( string[] args ) { Console.WriteLine( "Welcome to C# Programming!" ); }

Examples
using System; using System.Windows.Forms;
class Welcome4 { static void Main( string[] args ) { MessageBox.Show( "Welcome\nto\nC#\nprogramming!" ); } }

Examples(Adding 2 numbers)
using System; class Addition { static void Main( string[] args ) { string firstNumber, // first string entered by user secondNumber; // second string entered by user int number1, // first number to add number2, // second number to add sum; // sum of number1 and number2 Console.Write( "Please enter the first integer: " ); firstNumber = Console.ReadLine();

Examples(Adding 2 numbers)
Console.Write( "\nPlease enter the second integer: " ); secondNumber = Console.ReadLine(); number1 = Int32.Parse( firstNumber ); number2 = Int32.Parse( secondNumber ); sum = number1 + number2; Console.WriteLine( "\nThe sum is {0}.", sum );
}// end method Main }// end class Addition

Control Structures
goto elimination

3 types of selection structures : If(single-selection structure) If/else(double-selection structure) Switch Case(multiple-selection structure)

Control Structures
4 repetition structures while, do/while for foreach

Math Library
Console.WriteLine( Math.Sqrt( 900.0 ) );
If c1 = 13.0, d= 3.0 and f = 4.0, then the statement

Console.WriteLine( Math.Sqrt( c1 + d * f ) ); Abs( x ) Ceiling( x ) Cos( x ) Exp( x ) Floor( x )

Math Library
Log( x ) Max( x, y )

Min( x, y )
Pow( x, y ) Sin( x ) Tan( x )

Arrays
int[] c = new int[ 12 ]; int[,] b = new int[ 2, 2 ]; b[ 0, 0 ] = 1; b[ 0, 1 ] = 2; b[ 1, 0 ] = 3; b[ 1, 1 ] = 4;

int[,] b = { { 1, 2 }, { 3, 4 } };

Arrays
int[][] array2 = new int[ 3 ][]; array2[ 0 ] = new int[] { 1, 2 };
array2[ 1 ] = new int[] { 3 }; array2[ 2 ] = new int[] { 4, 5, 6 };

Lists (Add Values)


List<int> list = new List<int>(); list.Add(2); list.Add(3); list.Add(5); list.Add(7);

Lists (Looping)
List<int> list = new List<int>();

list.Add(2); list.Add(3); list.Add(7); foreach (int prime in list) { Console.WriteLine(prime); } for (int i = 0; i < list.Count; i++) { Console.WriteLine(list[i]); }

Lists (Clear List)


List<int> list = new List<int>();

list.Add(2); list.Add(3); list.Add(7); Console.WriteLine(list.Count); // 3 list.Clear(); Console.WriteLine(list.Count); // 0

Copy array to list


int[] arr = new int[3]; // New array with 3 elements

arr[0] = 2; arr[1] = 3; arr[2] = 5; List<int> list = new List<int>(arr); // Copy to List Console.WriteLine(list.Count); // 3 elements in List

Join string List


List<string> cities = new List<string>(); cities.Add("New York"); cities.Add("Mumbai"); cities.Add("Berlin"); cities.Add("Istanbul"); line string line = string.Join(",", cities.ToArray()); Console.WriteLine(line);

Insert
List<int> numbers= new List<int>(); // Example List

numbers.Add(1); // Contains 1 numbers.Add(3)// Contains: 1, 3 numbers.Insert(1, 2)// Contains: 1, 2, 3

Data Base

What is a Database?
A usually large collection of data organized especially

for rapid search and retrieval

Data Storage on Different Media

Electronic spreadsheet

Database Filing cabinet

Entity Relationship Model


Create an entity relationship diagram from

business specifications or narratives:


EMPLOYEE #* number * name o job title assigned to composed of DEPARTMENT #* number * name o location

Scenario . . . Assign one or more employees to a department . . . . . . Some departments do not yet have assigned employees . . .

Entity Relationship Modeling Conventions

Entity
Singular, unique name Uppercase

Attribute
Singular name Lowercase Mandatory marked with * Optional marked with o

EMPLOYEE #* number * name o job title

assigned to composed of

DEPARTMENT #* number * name o location

Unique identifier (UID)


Primary marked with # Secondary marked with (#)

Query languages
XQuery is a query language for XML data sources; XPath is a declarative language for navigating XML

documents; XSQL is a query language combining the power of XML and the power of SQL, it is currently under development

SQL SELECT Statement


Used to query or retrieve data from the database.

Syntax of SQL SELECT Statement: SELECT column_list FROM table-name


table-name is the name of the table from which the

information is retrieved. column_list includes one or more columns from which data is retrieved. The code within the brackets is optional.

SQL SELECT Statement


Example : Table Name: Student Details
id 100 101 102 103 104 first_name Rahul Anjali Stephen Shekar Priya last_name Sharma Bhagwat Fleming Gowda Chandra age 10 12 09 18 15 subject Science Maths Science Maths Economics games Cricket Football Cricket Badminton Chess

SELECT first_name FROM student_details;

OR SELECT first_name, last_name FROM student_details;

SQL WHERE Clause


Syntax of SQL WHERE Clause: WHERE {column or expression} comparison-operator value; column or expression - is the column of a table or a expression comparison-operator - operators like = < > etc. value - Any user value or a column name for comparison Examples: SELECT first_name, last_name FROM student_details WHERE id = 100; SELECT name, salary, salary*1.2 AS new_salary FROM employee WHERE salary*1.2 > 30000; Output: name salary new_salary
----------Hrithik Harsha Priya ---------35000 35000 30000 ---------------37000 37000 360000

SQL Comparison Keywords


SQL LIKE Operator: Is used to list all rows in a table whose column values match a specified pattern.
Example:

SELECT first_name, last_name FROM student_details WHERE first_name LIKE 'S%';


first_name ------------Stephen Shekar last_name -------------

id 100 101 102 103

first_name last_name Rahul Anjali Stephen Shekar Priya Sharma Bhagwat Fleming Gowda Chandra

age 10 12 09 18 15

subject Science Maths Science Maths Economics

games Cricket Football Cricket Badminton Chess

Fleming 104 Gowda

% :Substitutes with any number of characters. _ :signifies a single character

SQL Comparison Keywords


SQL BETWEEN ... AND Operator: Is used to compare data for a range of values.
Example:

SELECT first_name, last_name, age FROM student_details WHERE age BETWEEN 10 AND 15;
first_name ------------Rahul Anajali Shekar last_name ------------Sharma Bhagwat Gowda age -----10 12 15

id 100 101 102 103 104

first_name last_name Rahul Anjali Stephen Shekar Priya Sharma Bhagwat Fleming Gowda Chandra

age 10 12 09 18 15

subject Science Maths Science Maths Economics

games Cricket Football Cricket Badminton Chess

SQL Comparison Keywords


SQL IN Operator: Is used when you want to compare a column with more than one value. It is similar to an OR condition.
Example:

SELECT first_name, last_name, subject FROM student_details WHERE subject IN ('Maths', 'Science');
first_name ------------Anajali Shekar Rahul Stephen last_name ------------Bhagwat Gowda Sharma Fleming subject ---------Maths Maths Science Science

id 100 101 102 103 104

first_name last_name Rahul Anjali Stephen Shekar Priya Sharma Bhagwat Fleming Gowda Chandra

age 10 12 09 18 15

subject Science Maths Science Maths Economics

games Cricket Football Cricket Badminton Chess

SQL ORDER BY
is used to sort results either in ascending or descending order. Oracle sorts query results in ascending order by default. ORDER BY Syntax : SELECT column-list FROM table_name [WHERE condition] [ORDER BY column1 [, column2, .. columnN] [DESC]]; Example: SELECT name, salary FROM employee ORDER BY salary; SELECT name, salary FROM employee ORDER BY name, salary; The same as : SELECT name, salary FROM employee ORDER BY 1, 2;
SELECT name, salary FROM employee ORDER BY name, salary DESC; SELECT name, salary, salary*1.2 AS new_salary FROM employee WHERE salary*1.2

> 30000 ORDER BY new_salary DESC;

SQL GROUP Functions


are built-in SQL functions that operate on groups of rows and return one value for the entire group. These functions are: COUNT, MAX, MIN, AVG, SUM.

SQL COUNT (): This function returns the number of rows in the table that satisfies the condition specified in the WHERE condition

Example:
SELECT COUNT (*) FROM employee WHERE dept = 'Electronics'; SELECT COUNT (*) FROM employee;

SQL GROUP Functions


SQL MAX(): This function is used to get the maximum value from a column. Example: SELECT MAX (salary) FROM employee;

SQL MIN(): This function is used to get the minimum value from a column. Example: SELECT MIN(salary) FROM employee;

SQL GROUP Functions


SQL AVG(): This function is used to get the average value of a numeric column. Example: SELECT AVG (salary) FROM employee; SQL SUM(): This function is used to get the sum of a numeric column. Example: SELECT SUM (salary) FROM employee;

C Programming Under Linux

Signals
Each signal has an integer number that represents it

(1, 2 and so on), as well as a symbolic name that is usually defined in the file signal.h. Each signal may have a signal handler, which is a function that gets called when the process receives that signal. When a signal is sent to a process, the operating system stops the execution of the process, and forces it to call the signal handler function. When that signal handler returns, the process continues execution from wherever it stopped.

Signals
Here is a list of signals that are used with process control:
- SIGKILL: Kill a process (cannot be caught or ignored). - SIGSTOP: Stop a process (cannot be caught or ignored). - SIGSTP: Interactive stop (Ctrl-Z). - SIGCONT: Continue a stopped process. - SIGCHLD: Sent by child to its parent on exit - SIGINT: Interrupt a process (Ctrl-C), default action is

terminating the process.

Signals
There are 2 user defined signals SIGUSR1, and

SIGUSR2. These signals can be used by users for any purpose.

#include <stdio.h> #include <signal.h> void handler(int signum); main() { /* * Set the signal handler */ signal (SIGINT, handler); while(1) { } }

/* dummy wait */

/* * This the the signal handler for SIGINT signal */

void handler(int signum) { printf("OUCH....\nSignal #%d received\n",signum); }

Inter Process Communication


The Linux IPC (Inter-process communication)

facilities provide a method for multiple processes to communicate with one another. There are several methods of IPC available to Linux C programmers: SYSV style message queues SYSV style shared memory segments SYSV style semaphore sets Pipes

Message Queues
Message queues can be best described as an internal

linked list within the kernel's addressing space. Messages can be sent to the queue in order and retrieved from the queue in several different ways. Each message queue (of course) is uniquely identified by an IPC identifier.

Message Buffer
Each message should be saved in a msgbuf structure. This particular data structure can be thought of as a template for message data. The most basic message buffer is defined as follows: /* message buffer for msgsnd and msgrcv calls */struct msgbuf { long mtype; /* type of message */ char mtext[1]; /* message text */}; There are two members in the msgbuf structure: mtype: The message type, represented as a positive number. mtext: The message data itself

Creating and Accessing Message Queues


In order to create a new message queue, or access an

existing queue, the msgget() system call is used. PROTOTYPE: int msgget ( key_t key, int msgflg ); RETURNS: message queue identifier on success

Sending and Receiving Messages


Once we have the queue identifier, we can begin

performing operations on it. To deliver a message to a queue, you use the msgsnd system call: PROTOTYPE: int msgsnd ( int msqid, struct msgbuf *msgp, int msgsz, int msgflg ); RETURNS: 0 on success

Sending and Receiving Messages


Now let's turn the discussion to actually retrieving the

message from the queue. To do this, you use the msgrcv() system call: PROTOTYPE: int msgrcv ( int msqid, struct msgbuf *msgp, int msgsz, long mtype, int msgflg ); RETURNS: Number of bytes copied into message buffer

Shared Memory

Shared memory can best be described as the

mapping of an area (segment) of memory that will be mapped and shared by more than one process.

Creating and Accessing Shared Memory


In order to create a new shared memory segment, or

access an existing one, the shmget() system call is used. PROTOTYPE: int shmget ( key_t key, int size, int shmflg ) RETURNS: shared memory segment identifier on success

Attaching a Memory Segment


Once a process has a valid IPC identifier for a given

segment, the next step is for the process to attach or map the segment into its own addressing space. PROTOTYPE: int shmat ( int shmid, char *shmaddr, int shmflg); RETURNS: address at which segment was attached to the process, or -1 in case of error

You might also like