diff --git a/guide/english/csharp/class/index.md b/guide/english/csharp/class/index.md index 9360cab65b..22283f9c36 100644 --- a/guide/english/csharp/class/index.md +++ b/guide/english/csharp/class/index.md @@ -1,11 +1,10 @@ --- title: Class --- + ## Class - A class in C# is defined as a reference type. In order to instatiate a variable of a reference type, you must specify the `new` keyword, otherwise the variable will have the default value of `null`. See below for an example. - ```csharp // The value of variableOne is null at this point. NewClass variableOne; @@ -32,8 +31,8 @@ public class NewClass ``` A class is a prototype or blueprint from which objects are created. In C#, the class is defined by using the keyword `class`. A class is used to combine together some methods, properties, fields, events, and delegates into a single unit. A class may contain nested classes too. -#### Example: Consider the case of Employee Class below: +#### Example: Consider the case of Employee Class below: ```csharp using System; @@ -73,6 +72,51 @@ namespace CPrograms A class can inherit from one base class only. However, it can implement from more than one interface. +#### Example of inheriting from one class and 2 interfaces +```csharp +// base class: Human +public class Human { + public int Age; + + public Human (int age) { + Age = age; + } +} + +// first interface: Student +public interface Student { + int StudentNumber { get; set; } +} + +// second interface: Employee +public interface Employee { + int EmployeeNumber { get; set; } + string JobTitle { get; set; } +} + +// Example of class extending a class and using 2 interfaces +public class Person : Human, Student, Employee { + // new field for Person + public string Name; + + // needed to satisfy Student interface + public int StudentNumber { get; set; } + + // needed to satisfy Employee interface + public int EmployeeNumber { get; set; } + public string JobTitle { get; set; } + + // set the instance variables and pass the age to the base class + public Person(string name, int age, int studentNum, int employeeNum, string jobTitle) : base(age) + { + Name = name; + StudentNumber = studentNum; + EmployeeNumber = employeeNum; + JobTitle = jobTitle; + } +} +``` + ## More Information Read more about classes [here](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/class)