Web Server Programming
Download
Report
Transcript Web Server Programming
C#
Content
A Summary on the C# Language
Muzaffer DOĞAN - Anadolu University
2
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
Statements
Response.Redirect("NewPage.aspx");
x = y + 5;
a = b = c = 5;
Muzaffer DOĞAN - Anadolu University
5
Comments
// This is a comment
/*
This
is
a
multiline
comment
*/
Muzaffer DOĞAN - Anadolu University
6
Arrays
string[] 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
Properties
private string m_Username;
public string Username {
get {
return m_Username;
}
set {
m_Username = value;
}
}
Muzaffer DOĞAN - Anadolu University
9
Properties
public string Name {
get {
return TextBox1.Text;
}
set {
if (value.StartsWith("m")) {
TextBox1.Text = value;
}
}
}
Muzaffer DOĞAN - Anadolu University
10
Automatic Properties
public 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.0
You 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 fields
Can validate a field when it is assigned
Interface 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 Methods
private 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 Statement
if (a == 3)
TextBox1.Text = "three";
if (a == 3)
{
TextBox1.Text = "three";
Label1.Text = "four";
}
Muzaffer DOĞAN - Anadolu University
15
If, Else, Else If
if (a == 1)
Label1.Text = "one";
else if (a == 2)
Label1.Text = "two";
else
Label1.Text = "other";
Muzaffer DOĞAN - Anadolu University
16
Case Statement
int 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 Statement
string 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 Statement
enum 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 Loop
for (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 Loop
int[] 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 Loop
int n = 5;
while (n > 0)
{
Label1.Text += n.ToString() + "\r\n";
n--;
}
Muzaffer DOĞAN - Anadolu University
22
Do..While Loop
int grade = 0;
do
{
grade += DoNextHomework();
} while (grade < 100);
Muzaffer DOĞAN - Anadolu University
23
Exception Handling
try {
// 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 Operations
string 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 Conversion
double 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