260 likes | 310 Views
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;
E N D
Content • A Summary on the C# Language Muzaffer DOĞAN - Anadolu University
Introduction to C# Muzaffer DOĞAN - Anadolu University
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
Statements • Response.Redirect("NewPage.aspx"); • x = y + 5; • a = b = c = 5; Muzaffer DOĞAN - Anadolu University
Comments • // This is a comment • /* Thisisamultilinecomment*/ Muzaffer DOĞAN - Anadolu University
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
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
Properties • private string m_Username;public string Username { get { return m_Username; } set { m_Username = value; }} Muzaffer DOĞAN - Anadolu University
Properties • public string Name { get { return TextBox1.Text; } set { if (value.StartsWith("m")) { TextBox1.Text = value; } }} Muzaffer DOĞAN - Anadolu University
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
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
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
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
If Statement • if (a == 3) TextBox1.Text = "three"; • if (a == 3) { TextBox1.Text = "three"; Label1.Text = "four";} Muzaffer DOĞAN - Anadolu University
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
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
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
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
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
Foreach Loop • int[] arr = newint[]{5, 7, 9, 10, 3}; • foreach (int number in arr){ Label1.Text += number.ToString() + "<br/>";} Muzaffer DOĞAN - Anadolu University
While Loop • int n = 5; • while (n > 0){ Label1.Text += n.ToString() + "\r\n"; n--;} Muzaffer DOĞAN - Anadolu University
Do..While Loop • int grade = 0;do{ grade += DoNextHomework();} while (grade < 100); Muzaffer DOĞAN - Anadolu University
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
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
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