Classes

As already mentioned, C# is an object-oriented language and supports declaration of classes together with various members, including constructors, finalizers, constants, fields, properties, indexers, events, methods, and operators, as well as delegates. Moreover, classes support inheritance and implementing interfaces. Static, abstract, and virtual members are available, as well.

An example class is shown as follows:

public class Person { private string _location = string.Empty; public string Name { get; set; } public int Age { get; set; } public Person() => Name = "---"; public Person(string name, int age) { Name = name; Age = age; } public void Relocate(string location) { if (!string.IsNullOrEmpty(location)) { _location = location; ...

Get C# Data Structures and Algorithms now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.