charles university in prague faculty of mathematics and physics c# language &.net platform 2 nd...
DESCRIPTION
Implicitly Typed Local Variables Examples: var i = 5; var s = "Hello"; var d = 1.0; var numbers = new int[] {1, 2, 3}; var orders = new Dictionary (); Are equivalent to: int i = 5; string s = "Hello"; double d = 1.0; int[] numbers = new int[] {1, 2, 3}; Dictionary orders = new Dictionary (); Errors: var x; // Error, no initializer to infer type from var y = {1, 2, 3}; // Error, collection initializer not permitted var z = null; // Error, null type not permittedTRANSCRIPT
CHARLES UNIVERSITY IN PRAGUE
http://d3s.mff.cuni.cz/~jezek
faculty of mathematics and physics
C# Language & .NET Platform
2nd Lecture
Pavel Jež[email protected]
Some of the slides are based on University of Linz .NET presentations.© University of Linz, Institute for System Software, 2004
published under the Microsoft Curriculum License(http://www.msdnaa.net/curriculum/license_curriculum.aspx)
Putting C# into Perspective
C++ C# Java
Powerful (e.g. multiple inheritance)
Productive (e.g. lot of syntactic sugar)
Simple
Fast Safe + Less errors in team development, etc. (many features different from Java)
Safe
Not slow
Interoperable
(Source portable) (Binary portable) Binary portable
Implicitly Typed Local Variables
Examples:var i = 5;var s = "Hello";var d = 1.0;var numbers = new int[] {1, 2, 3};var orders = new Dictionary<int,Order>();
Are equivalent to:int i = 5;string s = "Hello";double d = 1.0;int[] numbers = new int[] {1, 2, 3};Dictionary<int,Order> orders = new Dictionary<int,Order>();
Errors:var x; // Error, no initializer to infer type fromvar y = {1, 2, 3}; // Error, collection initializer not permittedvar z = null; // Error, null type not permitted
Exception Hierarchy (excerpt)
ExceptionSystemException
ArithmeticExceptionDivideByZeroExceptionOverflowException...
NullReferenceExceptionIndexOutOfRangeExceptionInvalidCastException...
ApplicationException... user-defined exceptions...
IOExceptionFileNotFoundExceptionDirectoryNotFoundException...
WebException...
try Statement
FileStream s = null;try {
s = new FileStream(curName, FileMode.Open);...
} catch (FileNotFoundException e) {Console.WriteLine("file {0} not found", e.FileName);
} catch (IOException) {Console.WriteLine("some IO exception occurred");
} catch {Console.WriteLine("some unknown error occurred");
} finally {if (s != null) s.Close();
}
catch clauses are checked in sequential order.
finally clause is always executed (if present).
Exception parameter name can be omitted in a catch clause.
Exception type must be derived from System.Exception.If exception parameter is missing, System.Exception is assumed.
System.Exception
Propertiese.Message the error message as a string;
set by new Exception(msg);e.StackTrace trace of the method call stack as a stringe.Source the application or object that threw the exceptione.TargetSite the method object that threw the exception...e.InnerException should be always set if rethrowing another exception
Methodse.ToString() returns the name of the exception and the StackTrace...