To create a class in C#, you can use the class keyword followed by the name of the class. Here is an example of a simple class definition:
main.cs223 chars13 linesIn this example, we have defined a class called Person which has two public fields (name and age) and one public method (SayHello).
Fields are variables that hold data, and methods are functions that perform tasks. The public keyword means that these fields and methods can be accessed from outside the class.
Encapsulation means that the implementation details of a class are hidden from the user of the class, and they can only interact with the class through its public interface. In this example, we have not used encapsulation because the fields are public and can be accessed from outside the class.
Inheritance is a mechanism where one class can inherit properties and methods from another class. A derived class (also called a subclass) can inherit from a base class (also called a superclass) using the : symbol. For example:
main.cs167 chars10 linesIn this example, we have defined a class called Employee which inherits from the Person class. It adds one new field (jobTitle) and one new method (Work). The Employee class can now access the fields and methods of the Person class.
gistlibby LogSnag