C# Exceptions – Try..Catch

C# Exceptions

When executing C# code, different errors can occur: coding errors made by the programmer, errors due to wrong input, or other unforeseeable things.

When an error occurs, C# will normally stop and generate an error message. The technical term for this is: C# will throw an exception (throw an error).


Continue reading C# Exceptions – Try..Catch

C# Files

Working With Files

The File class from the
System.IO
namespace, allows us to work with files:

Example

using System.IO;  // include the System.IO namespace

File.SomeFileMethod();  // use the file class with methods

The File class has many useful methods for creating and getting information about files. For example: Continue reading C# Files

C# Enum

C# Enums

An enum is a special “class” that represents a group of constants (unchangeable/read-only variables).

To create an enum, use the enum keyword (instead of class or interface), and separate the enum items with a comma:

Example

enum Level 
{
  Low,
  Medium,
  High
}

You can access enum items with the dot syntax:

Level myVar = Level.Medium;
Console.WriteLine(myVar);

Enum is short for “enumerations”, which means “specifically listed”.

Continue reading C# Enum

C# Multiple Interfaces

Multiple Interfaces

To implement multiple interfaces, separate them with a comma:

Example

interface IFirstInterface 
{
  void myMethod(); // interface method
}

interface ISecondInterface 
{
  void myOtherMethod(); // interface method
}

// Implement multiple interfaces
class DemoClass : IFirstInterface, ISecondInterface 
{
  public void myMethod() 
  {
    Console.WriteLine("Some text..");
  }
  public void myOtherMethod() 
  {
    Console.WriteLine("Some other text...");
  }
}

class Program 
{
  static void Main(string[] args)
  {
    DemoClass myObj = new DemoClass();
    myObj.myMethod();
    myObj.myOtherMethod();
  }
}

Continue reading C# Multiple Interfaces

C# Interface

Interfaces

Another way to achieve abstraction in C#, is with interfaces.

An interface is a completely “abstract class“, which can only contain abstract methods and properties (with empty bodies):

Example

// interface
interface Animal 
{
  void animalSound(); // interface method (does not have a body)
  void run(); // interface method (does not have a body)
}

It is considered good practice to start with the letter “I” at the beginning of an interface, as it makes it easier for yourself and others to remember that it is an interface and not a class.

By default, members of an interface are abstract and public.

Note: Interfaces can contain properties and methods, but not fields.

Continue reading C# Interface

C# Abstraction

Abstract Classes and Methods

Data abstraction is the process of hiding certain details and showing only essential information to the user.
Abstraction can be achieved with either abstract classes or interfaces (which you will learn more about in the next chapter).

The abstract keyword is used for classes and methods:

    • Abstract class: is a restricted class that cannot be used to create objects (to access it, it must be inherited from another class).

 

  • Abstract method: can only be used in an abstract class, and it does not have a body. The body is provided by the derived class (inherited from).

Continue reading C# Abstraction

C# Polymorphism

Polymorphism and Overriding Methods

Polymorphism means “many forms”, and it occurs when we have many classes that are related to each other by inheritance.

Like we specified in the previous chapter; Inheritance lets us inherit fields and methods from another class. Polymorphism uses those methods to perform different tasks. This allows us to perform a single action in different ways.

For example, think of a base class called Animal that has a method called animalSound(). Derived classes of Animals could be Pigs, Cats, Dogs, Birds – And they also have their own implementation of an animal sound (the pig oinks, and the cat meows, etc.): Continue reading C# Polymorphism

C# Inheritance

Inheritance (Derived and Base Class)

In C#, it is possible to inherit fields and methods from one class to another. We group the “inheritance concept” into two categories:

  • Derived Class (child) – the class that inherits from another class
  • Base Class (parent) – the class being inherited from

To inherit from a class, use the : symbol.

In the example below, the Car class (child) inherits the fields and methods from the Vehicle class (parent): Continue reading C# Inheritance

C# Properties

Properties and Encapsulation

Before we start to explain properties, you should have a basic understanding of “Encapsulation“.

The meaning of Encapsulation, is to make sure that “sensitive” data is hidden from users. To achieve this, you must:

  • declare fields/variables as private
  • provide public get and set methods, through properties, to access and update the value of a private field

Continue reading C# Properties

C# Access Modifiers

Access Modifiers

By now, you are quite familiar with the public keyword that appears in many of our examples:

public string color;

The public keyword is an access modifier, which is used to set the access level/visibility for classes, fields, methods and properties.

C# has the following access modifiers:

Modifier Description
public The code is accessible for all classes
private The code is only accessible within the same class
protected The code is accessible within the same class, or in a class that is inherited from that class. You will learn more about inheritance in a later chapter
internal The code is only accessible within its own assembly, but not from another assembly. You will learn more about this in a later chapter

There’s also two combinations: protected internal and private protected. Continue reading C# Access Modifiers