08 Handout 1 Structs, Enums, Exceptions, and Files PDF
Document Details
Uploaded by WinningWaterfall6157
Tags
Summary
This document provides an overview of structs, enums, exceptions, and files in the C# programming language.
Full Transcript
SH1803 Structs, Enums, Exceptions, and Files I. Structs These are value types typically used to encapsulate small groups of related variables (for example, the x and y coordinates of a rectangle or the characteristics of an item in an inventory list)....
SH1803 Structs, Enums, Exceptions, and Files I. Structs These are value types typically used to encapsulate small groups of related variables (for example, the x and y coordinates of a rectangle or the characteristics of an item in an inventory list). Uses the struct keyword for its declaration. A. Differences Between Structs and Classes The syntax structs share most of the same components as those for classes but are more limited than classes. However, unlike classes, structs can be instantiated without using the new keyword. Structs CAN contain methods, properties, indexers, and so on. Structs CANNOT contain default constructors (a constructor without parameters), but they can have constructors that take parameters. The new keyword is used to instantiate a struct object, similar to class objects; this instantiation is entirely different from actual structs (ones created without using the new keyword). In general, classes are used to model more complex behavior or data that is intended to be modified after a class object is created, while structs are best suited for small data structures that contain primarily data that is not intended to be modified after the struct is created. Consider defining a struct instead of a class if you are trying to represent a simple set of data. II. Enums These are programming concepts that contain a set of named constants called enumerator list. Enumerator lists function similar to an array in that they contain elements and follow array indexing (index numbers for elements start at zero), except they contain constant values that cannot be changed or altered. Use the enum keyword for its declaration. Values in the enumeration list can be referred using the dot syntax (a dot after the name of the enumeration list) and the value of the constant to be called. In order to assign Enum values to variables, you have to specify the type in parentheses (for example, specify int if you want to assign constant int-type enumeration list constants to a standard variable). Index values for elements within the enumeration list can also be manually assigned; however, the element value of the next item in the enum list will be one (1) increment of the previous (the element with the manually assigned index) value. III. Exception Handling A. What are Exceptions? They are problems, mostly errors (either caused by the program, the user, or other unavailable resources such as memory, data, among others), that occur during program execution. They cause various problems for a program, erroneous data, data loss, and even program termination. 08 Handout 1 *Property of STI [email protected] Page 1 of 3 SH1803 The process of coding a program so that it would be able to provide possible solutions to an exception is called exception handling. B. The try-catch Statement A flexible mechanism in C# that allows a program to handle exceptions so a program won’t crash when an error occurs: o The try block contains code that might generate an expected exception. o The catch block executes relevant code for the captured exception in order to allow the program to continue running without problems. The catch block uses the special data type Exception and special variable letter e to access exception details such as the original error message. C. Handling Multiple Exceptions A single try block can contain multiple catch blocks that handle different exceptions separately. This allows exception handling to be particularly useful when dealing with user input. Multiple exceptions are usually defined by using existing, self-explanatory exception values: o FileNotFoundException refers to non-existing files. o FormatException refers to conflicting or non-existing data types and/or other formats. o IndexOutOfRangeException refers to non-existing array index values or index values that are over the element range of an array. o InvalidOperationException refers to any method call that is not allowed for an object’s current state (such as calling of a private class member). o OutOfMemoryException refers to any possible situation where there is not enough memory to continue the program’s execution. D. The finally Block It is an optional block in exception handling code that can be used after the catch block declarations. It is used to execute a given set of statements regardless of whether an exception is thrown or not (such as when the code needs to work on files and/or resources). IV. Working with Files A. The File Class It is one (1) of the various classes of the System.IO namespace. It can handle different types of file operations such as file creation and deletion, reading to and from a file, opening and closing of files for operations, etc. B. File Class Methods The WriteAllText() method creates a file (via its first parameter) with the specified path and writes the content to it (via its second parameter). If the file already exists, it is overwritten. The ReadAllText() method reads the contents of a file (supplied via its sole parameter). The contents can be assigned to a variable for program use. The AppendAllText() method appends text. Appended text is usually added at the end of the file’s contents. 08 Handout 1 *Property of STI [email protected] Page 2 of 3 SH1803 The Create() method creates a file at a specified location, directory, or path (set as a declared string variable parameter). The Delete() method deletes a file from the specified location, directory, or path (set as a declared string variable parameter). The Exists() method checks whether a file exists from the specified location, directory, or path (set as a declared string variable parameter). The Copy() method copies a file from a specified location, directory, or path (set as the first declared string variable parameter) to another location/directory/path (set as the second declared string variable parameter). The Move() method moves a file from a specified location, directory, or path (set as the first declared string variable parameter) to another location, directory, or path (set as the second declared string variable parameter). References Microsoft. (n.d.). File Class. Retrieved on August 6, 2018, from https://msdn.microsoft.com/en-us/library/system.io.file(v=vs.110).aspx Microsoft. (n.d.). Introducing Visual Studio.NET. Retrieved on May 8, 2018, from http://msdn.microsoft.com/en-uslibrary/fc6bk1f4(v=vs.71).aspx. SoloLearn. (n.d.). C# tutorial. Retrieved on July 17, 2018, from https://www.sololearn.com/Course/CSharp/ 08 Handout 1 *Property of STI [email protected] Page 3 of 3