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
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!"); }
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
Using Standard Exceptions There is already a large collection of exceptions defined in the System namespace Some exceptions for common programming problems
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
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
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
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
About Collections in C# Non-generic collections are defined in System.Collections Generic collections are defined in System.Collections.Generic
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
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
Reading Text Files Using C# Read the whole file using System.IO.File Read one line at a time using System.IO.StreamReader
Element Access Add, Remove, RemoveAt, RemoveAll … Dictionary.TryGetValue() The "Item" property in Dictionary and List More on indexer methods later
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
Readings Pro C# 7: Chapter 7, 8, 9