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

Software Development Technologies

 Programming Topics in C#
Last Class

• C# programs
– Basic C# programs

2 26, Sept, 2017


Outline

• Advance C#
– Making First Program in Visual Studio
– Regular Expression
– Attributes
– Collection
– Threading
– Delegates

3
Making First Project in C#

• Choose File from menubar


Making First Project in C#

• Select New-> Project


Making First Project in C#

• Choose Console App (.Net Framework)


• Assign name to your project and press ok
Making First Project in C#

• Your program looks like following


Regular Expression

• A regular expression is a pattern that could be


matched against an input text.
• Example
– To ensure that the text entered has all small
characters
• [a-z] matches small case alphabets
• [A-Z] matches capital alphabets
• [1-9] matches decimals

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

• Therefore, ^ restricts to start the string from


defined word.
• Similarly, placing \d at start of string

Output
Exp Passed

13 March 4, 2015
Regular Expresison

• Consider following program

Output
Exp Passed

14 March 4, 2015
Regular Expression

• Consider Following program

Output
Passed
Failed
Failed

15 March 4, 2015
Regular Expression

• Consider Following program

Output
Passed
Failed
Failed

16 March 4, 2015
Regular Expression

• Consider Following program

Output
Failed
Failed
Failed

17 March 4, 2015
Regular Expression

• Consider Following program

Output
Passed
Failed
Failed

18 March 4, 2015
Regular Expression

• Use $ at the end of expression to determine end


of string

Output
Passed
Failed
Failed

19 March 4, 2015
Regular Expression

• Controlling length of string

Output
Passed
Failed
Failed

20 March 4, 2015
Regular Expression

• The resulting regular expression to validate email


address is
– [\w\d]+@[\w\d]+.[\w\d]
• Regular expression for date 23-12-2009
– ([1-9]|1[0-9]|2[0-9]|3[0-1])[-](0[1-9]|1[012])[-
]([20]\d\d)
– [1-9] presents: 1,2,3,4,5,6,7,8,9
– 0[1-9] presents: 01,02,03,04,…..09
– 0[1-9]|1[0-9] presents: 01,02,03,….19
– 0[1-9]|1[0-9]|2[0-9]|3[0-1] presents: 01,02,….31
21
Example : Regular Expression
public void matchDatePatters()
{
string expression = "10-10-2015";
Regex regix = new Regex( @"(0[1-9]|1[0-9]|2[0-9]|3[0-1])[-](0[1-9]|1[012])[-
]([20]\d\d)");
if (regix.IsMatch(expression))
{
Console.Write(expression);
}
Console.WriteLine();
}

22
Attributes

• Attributes provide a powerful method of


associating declarative information with C# code.
– Declarative means “descriptive”
• Attributes can also work like a class meta-data.
– Meta-data means data about data.
– Consider the Employee class in last session
• The meta-data of Employee class are Fields & methods
(name, CNIC, age, void setName(string n) etc. ) their types
(name is of type string, age is of type double, return value
of setName(…) is void etc. ), the access Specifiers ( of name
is private, setName(..) method is public) and so on.

23
Attributes

• Once associated with program entity (class, field,


method etc.), the attribute can affect that
particular entity both compile time and runtime.

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;
}

[ Obsolete ("This method is depreciated, use


Multiply(double, double) instead", false)]
public float multiply(float v1, float v2)
{
return v1 * v2;
}

public double multiply(double v1, double v2) }


{ }
return v1 * v2;
} }
}
}

25
Collection

• Collection is a library in C# that contains different


type of data structures
• It belongs to System.Collection namespace e.g.
– using System.Collections
• Different kind of Collection classes includes
– ArrayList
– Stack
– SortedList
– Queue
– HashTable
– etc..

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

• A thread is an execution path that can proceed


independently of others.
• Thread runs within an operating system process.
• Each Process has one or multiple threads.
• Threads within program share resources .
• The resources can be
– Variables
– Memory
– Disk
– Files, network etc

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

• A delegate is an object that knows how to call a


method.
• A delegate instance takes a reference of a
method and has the ability to call that method.
• A delegate instance is similar to function pointer
in c#.
• Example of Delegate
– delegate void Transformer (int x)

32
Delegates

• Transformer is compatible with any method with


an int return type and a single int parameter,
such as this:
– static int Square (int x) { return x * x; }
• Assigning a method to a delegate variable
creates a delegate instance:
– Transformer t = Square;
• which can be invoked in the same way as a
method:
– int answer = t(3);
33
Delegate (Example)
delegate int Transformer (int x);
class Program
{

public static int Square (int x) {


return x * x;
}
static void Main() {
Program p=new Program();
p.Square(3);
Transformer t = Square;
// Create delegate instance
int result = t(3); // Invoke delegate
Console.WriteLine (result); // 9
}
}

34
Delegates

Value of Num: 35
35 March 25
Value of Num: 4, 2015
Next Class

• Application Development using MVC


• Language Integrated Query (LINQ)

36

You might also like