Polymorphism and Overriding Methods
Example
Example
Example
Why And When To Use “Inheritance” and “Polymorphism”?
Abstract Classes and Methods
- An abstract class must be inherited from another class in order to access it; it is a limited class that cannot be used to construct objects.
- Abstract methods don’t have bodies and can only be used in abstract classes. The derived class (inherited from) provides the body.
Example
Why And When To Use Abstract Classes and Methods?
Interfaces
Example
Members of an interface are public and abstract by default.
Note: Fields cannot be found in interfaces, but properties and methods can.
The interface needs to be “implemented”—akin to inherited—by another class in order for it to have access to its functions. Use the : symbol to implement an interface (exactly like with inheritance). The “implement” class provides the interface method’s body. It should be noted that when implementing an interface, the override keyword is not required:
Example
Notes on Interfaces:
- Interfaces, like abstract classes, are not meant to be used to construct objects; for instance, the Program class does not allow the creation of “IAnimal” objects.
- Interface methods lack a body; instead, the “implement” class provides the body.
- When an interface is implemented, all of its methods need to be overridden.
- Fields and variables cannot be found in interfaces, but properties and methods can.
- Members of an interface are by default public and abstract.
- A constructor is not allowed in an interface since it cannot be used to create objects.
Why And When To Use Interfaces?
2) A class can only inherit from one base class; “multiple inheritance” is not supported in C#. Nevertheless, since the class can implement numerous interfaces, it is possible to accomplish it with interfaces.
Note: Use commas to divide various interfaces that you want to implement (see example below).
Multiple Interfaces
Use commas to divide up various interfaces that you want to implement:
Example
C# Enums
Use the enum keyword (rather than class or interface) to build an enum, and use commas to divide the enum items:
Example
Enum inside a Class
Example
Enum Values
By default, the first item of an enum has the value 0. The second has the value 1, and so on.
To get the integer value from an item, you must explicitly convert the item to an int:
Example
Example
Enum in a Switch Statement
Example
Why And When To Use Enums?
When you have values (month days, days, colors, deck of cards, etc.) that you know will not change, use enums.
C# Files
Working With Files
Example
File
class has many useful methods for creating and getting information about files. For example:
METHOD | Description |
---|---|
AppendText() | Appends text at the end of an existing file |
Copy() | Copies a file |
Create() | Creates or overwrites a file |
Delete() | Deletes a file |
Exists() | Tests whether the file exists |
ReadAllText() | Reads the contents of a file |
Replace() | Replaces the contents of a file with the contents of another fileWrite |
AllText() | Creates a new file and writes the contents to it. If the file already exists, it will be overwritten. |
Write To a File and Read It
In the example that follows, we create a file called “filename.txt” and add some content to it using the WriteAllText() method. Next, we read the file’s contents using the ReadAllText() method:
Example
C# Exceptions – Try..Catch
Several types of errors can arise when running C# code, including programmer-made errors, problems brought on by incorrect input, and other unforeseen circumstances.
Normally, C# will halt and provide an error message when an error occurs. This is known technically as C# throwing an exception (throw an error).
C# try and catch
You can specify a section of code to be tested for errors during execution using the try statement.
You can provide a block of code that will be run in the event that the try block has an error by using the catch statement.
The keywords “try” and “catch” appear in pairs:
Syntax
If something goes wrong, we can use try…catch to identify the problem and run some code to fix it.
The variable inside the catch block (e) and the built-in Message property are used in the example below to produce a message that explains the exception:
Example
You can also output your own error message:
try
{
int[] myNumbers = {1, 2, 3};
Console.WriteLine(myNumbers[10]);
}
catch (Exception e)
{
Console.WriteLine(“Something went wrong.”);
}
The output will be :
Something went wrong
Finally
try
{
int[] myNumbers = {1, 2, 3};
Console.WriteLine(myNumbers[10]);
}
catch (Exception e)
{
Console.WriteLine(“Something went wrong.”);
}
finally
{
Console.WriteLine(“The ‘try catch’ is finished.”);
}
The output will be:
Something went wrong.
The ‘try catch’ is finished.
The throw keyword
The throw
statement allows you to create a custom error.
The throw
statement is used together with an exception class. There are many exception classes available in C#: ArithmeticException
, FileNotFoundException
, IndexOutOfRangeException
, TimeOutException
, etc:
Example
age
was 20, you would not get an exception: