# C Sharp Programming/Classes

As in other object-oriented programming languages, the functionality of a C# program is implemented in one or more classes. The methods and properties of a class contain the code that defines how the class behaves.
C# classes support information hiding by encapsulating functionality in properties and methods and by enabling several types of polymorphism, including subtyping polymorphism via inheritance and parametric polymorphism via generics.
Several types of C# classes can be defined, including instance classes (standard classes that can be instantiated), static classes, and structures.
Classes are defined using the keyword class followed by an identifier to name the class. Instances of the class can then be created with the new keyword followed by the name of the class.
The code below defines a class called Employee with properties Name and Age and with empty methods GetPayCheck and Work. It also defines a Sample class that instantiates and uses the Employee class:

## Methods

C# methods are class members containing code. They may have a return value and a list of parameters, as well as a generic type declaration. Like fields, methods can be static (associated with and accessed through the class) or instance (associated with and accessed through an object instance of the class methods as well as a generic type declaration.
From C# 4.0 onwards, it is possible for a method to have optional parameters with default values, as users of C++ already know. For example, the method
can be called with one parameter only, as the second parameter, dx, is initialised to a default value.

## Constructors of classes

A class's constructors control its initialization. A constructor's code executes to initialize an instance of the class when a program requests a new object of the class's type. Constructors often set properties of their classes, but they are not restricted to doing so.
Like other methods, a constructor can have parameters. To create an object using a constructor with parameters, the new command accepts parameters. The code below defines and then instantiates multiple objects of the Employee class, once using the constructor without parameters and once using the version with a parameter:
Output:
Constructors can call each other:

## Finalizers (Destructors)

The opposite of constructors, finalizers define the final behavior of an object and execute when the object is no longer in use. Although they are often used in C++ to free resources reserved by an object, they are less frequently used in C# due to the .NET Framework Garbage Collector. An object's finalizer, which takes no parameters, is called sometime after an object is no longer referenced, but the complexities of garbage collection make the specific timing of finalizers uncertain.
Output:

## Properties

C# properties are class members that expose functionality of methods using the syntax of fields. They simplify the syntax of calling traditional get and set methods (a.k.a. accessor methods). Like methods, they can be static or instance.
Properties are defined in the following way:
An even shorter way for getter/setter methods are accessors that do both in one line:
The code is equivalent to a GetLanguage and SetLanguage method definition, but without having to define these methods. The user can directly access the member, if it is not private, of course.
The C# keyword value contains the value assigned to the property. After a property is defined it can be used like a variable. If you were to write some additional code in the get and set portions of the property it would work like a method and allow you to manipulate the data before it is read or written to the variable.
Using properties in this way provides a clean, easy to use mechanism for protecting data.

## Indexers

C# indexers are class members that define the behavior of the array access operation (e.g. list[0] to access the first element of list even when list is not an array).
To create an indexer, use the this keyword as in the following example:
This code will create a string indexer that returns a string value. For example, if the class was EmployeeCollection, you could write code similar to the following:

## Events

C# events are class members that expose notifications to clients of the class. Events are only fired and never assigned.
See also here for details.

## Operator overloading

C# operator definitions are class members that define or redefine the behavior of basic C# operators (called implicitly or explicitly) on instances of the class:

## Structures

Structures, or structs, are defined with the struct keyword followed by an identifier to name the structure. They are similar to classes, but have subtle differences. Structs are used as lightweight versions of classes that can help reduce memory management efforts when working with small data structures. In most situations, however, using a standard class is a better choice.
The principal difference between structs and classes is that instances of structs are values whereas instances of classes are references. Thus when you pass a struct to a function by value you get a copy of the object so changes to it are not reflected in the original because there are now two distinct objects but if you pass an instance of a class by reference then there is only one instance.
The Employee structure below declares a public and a private field. Access to the private field is granted through the public property Name:
Since C# 2.0, is possible to have arrays inside structures, but only in unsafe contexts:
The array is accessed using pointer arithmetic. Values are treat arrayed values as if they were C-style arrays using indexing, etc.

### Structure constructors

Structures need constructors - or better to say initialisers, as they do not construct but just initialise the memory - so that their contents are not left uninitialised. Therefore, constructors without parametres are not allowed.
Structure variables can be assigned one to another if and only if the structure variable on the right side of the assignment are all initialised.

## Static classes

Static classes are commonly used to implement a Singleton Pattern. All of the methods, properties, and fields of a static class are also static (like the WriteLine method of the System.Console class) and can thus be used without instantiating the static class:

## References

* ↑ [[[w:C_Sharp_syntax#Optional_parameters|Optional parameters]] "Optional parameters"]. Retrieved 2018-02-01. {{cite web}}: Check |url= value (help)
* ↑ Greg Beech (2008-06-16). "Structure constructors". http://social.msdn.microsoft.com/: MSDN. Retrieved 2012-04-12. Because structures are simply an inline area of memory, they cannot be null, and so the CLR has to be able to ensure that the area of memory is totally initialized rather than being partly garbage. For this reason, you'll often hear the 'constructors' on structures called (arguably more correctly) 'initializers' because the don't construct an object they just initialize an area of memory. {{cite web}}: External link in |location= (help)
* ↑ John Sharp. "Microsoft® Visual C#® 2005 Step by Step / Copying Structure Variables". http://books.google.at/: Google Books. Retrieved 2012-04-12. You're allowed to initialize or assign one struct variable to another struct variable, but only if the struct variable on the right side is completely initialized (that is, if all its fields are initialized). {{cite web}}: External link in |location= (help)