Upload
chaim-freeman
View
17
Download
2
Embed Size (px)
DESCRIPTION
Web Server Programming. C#. Content. A Summary on the C # Language. Introduction to C#. Variable Declarations in C#. int x; int x = 5; string s; string s1, s2; object o; object obj = new object(); public string name;. Statements. Response.Redirect("NewPage.aspx"); x = y + 5; - PowerPoint PPT Presentation
Citation preview
Web Server ProgrammingC#
ContentA Summary on the C# Language
Muzaffer DOĞAN - Anadolu University 2
Introduction to C#
Muzaffer DOĞAN - Anadolu University 3
Variable Declarations in C#int x;int x = 5;string s;string s1, s2;object o;object obj = new object();public string name;
Muzaffer DOĞAN - Anadolu University 4
StatementsResponse.Redirect("NewPage.aspx");x = y + 5;a = b = c = 5;
Muzaffer DOĞAN - Anadolu University 5
Comments// This is a comment/* Thisisamultilinecomment*/
Muzaffer DOĞAN - Anadolu University 6
Arraysstring[] course = new string[3];course[0] = "Web";course[1] = "Server";course[2] = "Programming";
int[] arr = new int[5]{1, 2, 3, 4, 5};int[][] mat = new int[2][3];mat[1][2] = 5;
int [,] mat = new int[3,4];mat[2,3] = 5;
char[] delimeters = new char[]{' ', '\r', '\n'};
Muzaffer DOĞAN - Anadolu University 7
Indexed Properties// Creating arrays:int[] arr = new int[100];int[,] mat = new int[50, 100];
// Accessing array elements:int x = arr[2];arr[0] = 5;string name = Request.QueryString["name"];
Muzaffer DOĞAN - Anadolu University 8
Propertiesprivate string m_Username;
public string Username { get { return m_Username; } set { m_Username = value; }}
Muzaffer DOĞAN - Anadolu University 9
Propertiespublic string Name { get { return TextBox1.Text; } set { if (value.StartsWith("m")) { TextBox1.Text = value; } }}
Muzaffer DOĞAN - Anadolu University 10
Automatic Propertiespublic string Username {get; set;}
A private variable to hold the value of the property is automatically created by the compiler
It can be used after C# 3.0You can create this type of properties in
Visual Studio by typing prop and pressing TAB key two times
Muzaffer DOĞAN - Anadolu University 11
Why Properties?Allows read-only and write-only fieldsCan validate a field when it is assignedInterface and implementation of data my
differ (i.e., value can be written into a Label control)
Visual Studio IDE can list properties of an object when dot-key is pressed (intellisense)
Muzaffer DOĞAN - Anadolu University 12
Enumerations// Declare the enumeration:public enum MessageSize { Small = 0, Medium = 1, Large = 2}
// Create a field or property:MessageSize msgSize;
// Assign a value:msgSize = MessageSize.Medium;
Muzaffer DOĞAN - Anadolu University 13
Declaring and Calling Methodsprivate int Add(int a, int b) { return a + b;}
public void PrintHeader() { Console.WriteLine("This is a header");}
int z = Add(x, y);PrintHeader();
Muzaffer DOĞAN - Anadolu University 14
If Statementif (a == 3) TextBox1.Text = "three";
if (a == 3){ TextBox1.Text = "three"; Label1.Text = "four";}
Muzaffer DOĞAN - Anadolu University 15
If, Else, Else Ifif (a == 1) Label1.Text = "one";else if (a == 2) Label1.Text = "two";else Label1.Text = "other";
Muzaffer DOĞAN - Anadolu University 16
Case Statementint num = 5;switch (num) { case 1: Label1.Text = "one"; break; case 2: case 3: Label1.Text = "two or three"; break; default: Label1.Text = "other"; break;}
Muzaffer DOĞAN - Anadolu University 17
Case Statementstring color = "blue";switch (color) { case "red": Label1.Text = "Red"; break; case "blue": Label1.Text = "Blue"; break; default: Label1.Text = "Not Red Nor Blue"; break;}
Muzaffer DOĞAN - Anadolu University 18
Case Statementenum MessageSize {Small, Medium, Large};MessageSize msgSize = MessageSize.Small;switch (msgSize) { case MessageSize.Small: Label1.Text = "S";
break; case MessageSize.Medium: Label1.Text = "M";
break; case MessageSize.Large: Label1.Text = "L";
break; default: Label1.Text = "---";
break;}
Muzaffer DOĞAN - Anadolu University 19
For Loopfor (int i = 0; i < 10; i++) single_statement();
for (int i = 0; j < 10; k %= 2) { statement1(); statement2();}
Muzaffer DOĞAN - Anadolu University 20
Foreach Loopint[] arr = new int[]{5, 7, 9, 10, 3};
foreach (int number in arr){ Label1.Text += number.ToString() + "<br/>";}
Muzaffer DOĞAN - Anadolu University 21
While Loopint n = 5;while (n > 0){ Label1.Text += n.ToString() + "\r\n"; n--;}
Muzaffer DOĞAN - Anadolu University 22
Do..While Loopint grade = 0;do{ grade += DoNextHomework();} while (grade < 100);
Muzaffer DOĞAN - Anadolu University 23
Exception Handlingtry { // Code that throws exception} catch (OverflowException e) { // Catch a specific exception} catch (Exception e) { // Catch the generic exceptions} finally { // Execute some cleanup code}
Muzaffer DOĞAN - Anadolu University 24
String Operationsstring s1 = "Web";string s2 = "Programming";string s3 = s1 + " " + s2;s3 += " (Server Side)";
// Use StringBuilder class for performance:StringBuilder s3 = new StringBuilder();s3.Append("Web");s3.Append(" Programming");
Muzaffer DOĞAN - Anadolu University 25
Type Casting and Conversiondouble d = 5.4321;
int n = (int)d;
string s = n.ToString();
int n = int.Parse("54321");
double d = double.Parse("54.321");
Muzaffer DOĞAN - Anadolu University 26