CODEX

In C#, we can find some useful OOP tools to make our code better.

In this article, you will find some intermediate tools that you can use in your program.

NOTE — The operations (operator overloading, indexers) would work for structs as well. However, the examples would use classes.

Image for post
Image for post

Indexers

If you have ever used List<T> or Dictionary<T> and you were curious about how can an instance of a class can have indices just like an array, then the answer for this is indexers.

In the example below, we can see how to create an indexer.

T — Stands for the value we return from the index.

[int index] — The index the user gave…


Image for post
Image for post
Photo by Oskar Yildiz on Unsplash

Any programming language has its tricks and purposes.

Understanding the tricks behind any programming language can make your code to be shorter and more efficient.

Expression-bodied members in Classes

In C# OOP we can find some useful tools that can save us a lot of time and place while writing code.

public class SampleClass
{
public int x { get; set; }
public int y { get; set; }
public int z => x + y; //automaticlly holds the sum
}

In the following example, we can see two basic declarations of variables.

The declaration of the x & y variable means that we can get & set their values from anywhere.

Gilad Bar Ilan

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store