Professional Documents
Culture Documents
Software Development Technologies: Programming Topics in C#
Software Development Technologies: Programming Topics in C#
Programming Topics in C#
Last Class
• C# programs
– Basic C# programs
• Advance C#
– Making First Program in Visual Studio
– Regular Expression
– Attributes
– Collection
– Threading
– Delegates
3
Making First Project in C#
8 March 4, 2015
Regular Expression
class Program
{
static void Main(string[] args)
{
string[] text = new string[3];
text[0]="asad@yahoo.com";
text[1] = "asadyahoo.com";
text[2]= "asad@yahoo";
foreach (string txt in text)
{
if (Regex.IsMatch(txt, "[a-z]+@[a-z]+.com"))
Console.WriteLine("passed");
else
Console.WriteLine("Failed");
}
Console.ReadLine();
}
9 March 4, 2015
Regular Expression
• Anchors
– The caret '^' and dollar '$' characters have special
meaning in a regexp. They are called “anchors”.
– The caret ^ matches at the beginning of the text, and
the dollar $ – in the end.
• \s
– Find if white space exist
• ? Or *
– At least zero occurrence
• +
– At least one occurrence
10 March 4, 2015
Regular Expression
• Example
class Program
{
static void Main(string[] args)
{
string exp = "Asad has a marker and folder";
string regExp = @"^Asad\s[\w]?";
if (Regex.IsMatch(exp, regExp))
{
Console.WriteLine("Passed");
}
else
{
Console.WriteLine("Failed");
}
Console.ReadLine();
}
}
11 March 4, 2015
Regular Expression
• What happens if we remove caret from start of regular ^ expression?
class Program
{
static void Main(string[] args)
{
string exp = "hello Asad has a marker and folder";
string regExp = @"Asad\s[\w]?";
if (Regex.IsMatch(exp, regExp))
{
Console.WriteLine("Passed");
}
else
{
Console.WriteLine("Failed");
}
Console.ReadLine();
} Output:
} Passed
12 March 4, 2015
Regular Expression
Output
Exp Passed
13 March 4, 2015
Regular Expresison
Output
Exp Passed
14 March 4, 2015
Regular Expression
Output
Passed
Failed
Failed
15 March 4, 2015
Regular Expression
Output
Passed
Failed
Failed
16 March 4, 2015
Regular Expression
Output
Failed
Failed
Failed
17 March 4, 2015
Regular Expression
Output
Passed
Failed
Failed
18 March 4, 2015
Regular Expression
Output
Passed
Failed
Failed
19 March 4, 2015
Regular Expression
Output
Passed
Failed
Failed
20 March 4, 2015
Regular Expression
22
Attributes
23
Attributes
24
Compile Time Attribute (Example)
namespace ClassActivities namespace ClassActivities
{
class Calculator {
{ class Program
{
[ Obsolete ("This method is depreciated, use
Multiply(double, double) instead", true)] static void Main(string[] args)
public int multiply(int v1, int v2) {
{
return v1 * v2;
}
25
Collection
26
Collection
Class CollectionTest //Hash Table
{ public void testHashTable()
//Array Operations {
public void testArrayList() Hashtable hashTable = new
{ Hashtable();
ArrayList array = new ArrayList(); //add item
//add item hashTable.Add(1, “book”);
array.Add("laptop");
//get item hashTable.Add(2, new
Console.Write(array[0].ToString()); Employee("saeed", "345-4545343-34",
//remove item 23, 6.0));
array.Remove("laptop");
//clear array hashTable.Add(6, new
array.Add("laptop"); Doctor("saad", "32343-343434-34", 42,
array.Add("TV"); 5.5,"Asst Prof"));
array.Add("computer"); //call methods to remove, clear, count
array.Clear(); etc.
} }
27
Employee Class
using System; public void SetName(string name) {
using System.Collections.Generic; this.Name = name;
using System.Linq;
}
using System.Text;
using System.Threading.Tasks; public void SetAge(double age) {
this.Age = age;
namespace Assignment01 { }
class Employee { public string GetCNIC() {
private String CNIC; return CNIC;
private String Name;
private double Age; }
public Employee(){ public string GetName() {
} return this.Name;
public Employee(String name, string }
cnic, double age) { public double GetAge() {
this.Name = name; return this.Age;
this.CNIC = cnic;
this.Age = age; }
} } //end namespace
public void SetCNIC(string cnic) { } //end class
this.CNIC = cnic;
}
28 March 4, 2015
Collection:Dictionary
//Dictionary Operations
public void testIDictionary()
{
Dictionary<int,Employee> dictionary = new Dictionary<int,Employee>();
dictionary.Add(1,new Employee("Ameen", "342-234234-324", 33));
dictionary.Add(2,new Employee("saeed", "345-4545343-34", 23));
foreach (var dict in dictionary)
{
Employee emp = (Employee)dict.Value;
}
}
}//End class
29
Threading
30
Threading
class ThreadProgram using System.Threading;
{ class Program
public void Method1() {
{ static void Main(string[] args)
for (int i = 0; i < 1000; i++) {
{ ThreadProgram t = new
Console.WriteLine("thread 1 :"+i); ThreadProgram();
}
} //end method Thread thread1 = new
public void Method2 () Thread(t.Method1);
{
for (int i = 1000; i < 2000; i++) Thread thread2 = new
{ Thread(t.Method2);
Console.WriteLine("thread 2 :" + i);
} thread1.Start();
} //end method thread2.Start();
} //end class }
}
31
Delegates
32
Delegates
34
Delegates
Value of Num: 35
35 March 25
Value of Num: 4, 2015
Next Class
36