Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chengyu Sun California State University, Los Angeles

Similar presentations


Presentation on theme: "Chengyu Sun California State University, Los Angeles"— Presentation transcript:

1 Chengyu Sun California State University, Los Angeles
CS4540 Special Topics in Web Development C# for Java Programmers: Exceptions and Collections Chengyu Sun California State University, Los Angeles

2 Exception Handling Exception handling is mostly like in Java
No checked exception in C# (Yay!) static void FutureFeature() {throw new NotImplementedException(); } static void Main(string[] args) { try { FutureFeature(); } catch (NotImplementedException e) { Console.WriteLine(e.Message); } finally { Console.WriteLine("Done!"); }

3 What's Inside an Exception
Important properties in Exception Message StackTrace TargetSite: can be used to obtain information about the method that threw the exception Data: additional data (a collection of key-value pairs) associated with the exception

4 Using Standard Exceptions
There is already a large collection of exceptions defined in the System namespace Some exceptions for common programming problems

5 Creating A Custom Exception
Minimum for a custom exception Inherit from Exception or ApplicationException class Have a no-argument constructor Have a constructor that takes a string message Or, use the ex code snippet in Visual Studio

6 Common Collections in Java
Interface Class Characteristics List ArrayList, LinkedList Ordered; allow duplicates Set HashSet, LinkedHashSet, TreeSet Un-ordered or ordered in a specific way; no duplicates Map HashMap, LinkedHashMap, TreeMap Key-Value pairs Queue, Deque PriorityQueue, Stack Common data structures

7 Generics Type Parameter public class GenericList<T> { public void Add(T input) { } } var list = new GenericList<int>(); Type Argument Most commonly used to ensure all the elements in a collection is of the same type

8 Why Not Use Collection of Objects
Performance: has to convert back and forth between value types and reference types (aka boxing/unboxing) Type safety Cannot prevent different types of data getting into the same collection Require explicit cast when accessing data

9 About Collections in C#
Non-generic collections are defined in System.Collections Generic collections are defined in System.Collections.Generic

10 Common Collections in C#
Interface Class Characteristics IList List, LinkedList, SortedList Ordered; allow duplicates ISet HashSet, SortedSet Un-ordered or ordered in a specific way; no duplicates IDictionary Dictionary, SortedDictionary Key-Value pairs Queue, Stack Common data structures

11 Example: English-Spanish Translator
Download the dictionary file from the Internet Dictionary Project Read the file and store the dictionary in a Dictionary<string, List<string>> Get an English word from input and displays its Spanish translations

12 Reading Text Files Using C#
Read the whole file using System.IO.File Read one line at a time using System.IO.StreamReader

13 Element Access Add, Remove, RemoveAt, RemoveAll …
Dictionary.TryGetValue() The "Item" property in Dictionary and List More on indexer methods later

14 A Couple of Important Interfaces
IEnumerable<T>: foreach can be used on anything implementing this interface ICollection<T> Implements IEnumerable<T> Count, Add, Remove, Contains

15 Readings Pro C# 7: Chapter 7, 8, 9


Download ppt "Chengyu Sun California State University, Los Angeles"

Similar presentations


Ads by Google