Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
C# Language Overview Part II
1. C# Language Overview (Part II) Creating and Using Objects, Exceptions, Strings, Generics, Collections, Attributes Doncho Minkov Telerik School Academy schoolacademy.telerik.com Technical Trainer http://www.minkov.it
6. Classes – Example Account +Owner: Person +Ammount: double +Suspend() +Deposit(sum:double) +Withdraw(sum:double) Class Name Attributes (Properties and Fields) Operations (Methods)
7.
8. Objects – Example Account +Owner: Person +Ammount: double +Suspend() +Deposit(sum:double) +Withdraw(sum:double) Class ivanAccount +Owner="Ivan Kolev" +Ammount=5000.0 peterAccount +Owner="Peter Kirov" +Ammount=1825.33 kirilAccount +Owner="Kiril Kirov" +Ammount=25.0 Object Object Object
9.
10.
11.
12.
13.
14.
15.
16. Accessing Properties and Fields – Example using System; ... DateTime christmas = new DateTime(2009, 12, 25); int day = christmas.Day; int month = christmas.Month; int year = christmas.Year; Console.WriteLine( "Christmas day: {0}, month: {1}, year: {2}", day, month, year); Console.WriteLine( "Day of year: {0}", christmas.DayOfYear); Console.WriteLine("Is {0} leap year: {1}", year, DateTime.IsLeapYear(year));
37. Handling Exceptions – Example static void Main() { string s = Console.ReadLine(); try { Int32.Parse(s); Console.WriteLine( "You entered valid Int32 number {0}.", s); } catch (FormatException) { Console.WriteLine("Invalid integer number!"); } catch (OverflowException) { Console.WriteLine( "The number is too big to fit in Int32!"); } }
54. Strings – Example static void Main() { string s = "Stand up, stand up, Balkan Superman."; Console.WriteLine("s = amp;quot;{0}amp;quot;", s); Console.WriteLine("s.Length = {0}", s.Length); for (int i = 0; i < s.Length; i++) { Console.WriteLine("s[{0}] = {1}", i, s[i]); } }
55.
56.
57.
58.
59.
60.
61.
62.
63. Searching in Strings – Example string str = "C# Programming Course"; int index = str.IndexOf("C#"); // index = 0 index = str.IndexOf("Course"); // index = 15 index = str.IndexOf("COURSE"); // index = -1 // IndexOf is case-sensetive. -1 means not found index = str.IndexOf("ram"); // index = 7 index = str.IndexOf("r"); // index = 4 index = str.IndexOf("r", 5); // index = 7 index = str.IndexOf("r", 8); // index = 18 index = s[index] = 0 1 2 3 4 5 6 7 8 9 10 11 12 13 … C # P r o g r a m m i n g …
82. List<T> – Simple Example static void Main() { List<string> list = new List<string>(); list.Add("C#"); list.Add("Java"); list.Add("PHP"); foreach (string item in list) { Console.WriteLine(item); } // Result: // C# // Java // PHP }
83.
84.
85. Primes in an Interval – Example static List<int> FindPrimes(int start, int end) { List<int> primesList = new List<int>(); for (int num = start; num <= end; num++) { bool prime = true; for (int div = 2; div <= Math.Sqrt(num); div++) { if (num % div == 0) { prime = false; break; } } if (prime) { primesList.Add(num); } } return primesList; }
86.
87.
88.
89.
90.
91.
92.
93. Dictionary<TKey,Tvalue> – Example Dictionary<string, int> studentsMarks = new Dictionary<string, int>(); studentsMarks.Add("Ivan", 4); studentsMarks.Add("Peter", 6); studentsMarks.Add("Maria", 6); studentsMarks.Add("George", 5); int peterMark = studentsMarks["Peter"]; Console.WriteLine("Peter's mark: {0}", peterMark); Console.WriteLine("Is Peter in the hash table: {0}", studentsMarks.ContainsKey("Peter")); Console.WriteLine("Students and grades:"); foreach (var pair in studentsMarks) { Console.WriteLine("{0} --> {1} ", pair.Key, pair.Value); }
94. Counting Words in Given Text string text = "Welcome to our C# course. In this " + "course you will learn how to write simple " + "programs in C# and Microsoft .NET"; string[] words = text.Split(new char[] {' ', ',', '.'}, StringSplitOptions.RemoveEmptyEntries); var wordsCount = new Dictionary<string, int>(); foreach (string word in words) { if (wordsCount.ContainsKey(word)) wordsCount[word]++; else wordsCount.Add(word, 1); } foreach (var pair in wordsCount) { Console.WriteLine("{0} --> {1}", pair.Key, pair.Value); }
95.
96. Sorted Dictionary – Example string text = "Welcome to our C# course. In this " + "course you will learn how to write simple " + "programs in C# and Microsoft .NET"; string[] words = text.Split(new char[] {' ', ',', '.'}, StringSplitOptions.RemoveEmptyEntries); var wordsCount = new SortedDictionary<string, int>(); foreach (string word in words) { if (wordsCount.ContainsKey(word)) wordsCount[word]++; else wordsCount.Add(word, 1); } foreach (var pair in wordsCount) { Console.WriteLine("{0} --> {1}", pair.Key, pair.Value); }
* 07/16/96 (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
* 07/16/96 (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
* 07/16/96 (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
* (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
* (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
* (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
* (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
* (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
* (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
* (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
* (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
* (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
* (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
* (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
* (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
* (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
* (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ## Introducing the StringBuffer Class StringBuffer represents strings that can be modified and extended at run time. The following example creates three new String objects, and copies all the characters each time a new String is created: String quote = &quot;Fasten your seatbelts, &quot;; quote = quote + &quot;it’s going to be a bumpy night.&quot;; It is more efficient to preallocate the amount of space required using the StringBuffer constructor, and its append() method as follows: StringBuffer quote = new StringBuffer(60); // alloc 60 chars quote.append(&quot;Fasten your seatbelts, &quot;); quote.append(&quot; it’s going to be a bumpy night. &quot;); StringBuffer also provides a number of overloaded insert() methods for inserting various types of data at a particular location in the string buffer. Instructor Note The example in the slide uses StringBuffer to reverse the characters in a string. A StringBuffer object is created, with the same length as the string. The loop traverses the String parameter in reverse order and appends each of its characters to the StringBuffer object by using append() . The StringBuffer therefore holds a reverse copy of the String parameter. At the end of the method, a new String object is created from the StringBuffer object, and this String is returned from the method .
* (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
* (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
* (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
* (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
* (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##