This is my fifth article on C# 7. Followings are the links for my previous articles.
- Top 10 New Features of C# 7 With Visual Studio 2017
- Visual Studio 15 Preview First Look & C# 7
- How to Compile & Test C# 7 Features
- Understanding ref and out With C# 7
In C# 7, indexers and operator overloading can be written as expression bodied members.
Expression bodied indexers
In C# 7, Indexers can be written as expression bodied members called as “Expression bodied indexers”.

code snippet
- class MyFavColor
- {
- private Color[] favColor = new Color[] {Red, Green, Blue,Yellow, Orange};
- public string this[int index] => favColor[index].Name;
- }
Complete code with its usage
- using System.Drawing;
- using static System.Drawing.Color;
- using static System.Console;
- using Humanizer;
- using static Humanizer.OrdinalizeExtensions;
- namespace ExpressionBodiedIndexersExample
- {
- class Program
- {
- static void Main(string[] args)
- {
- MyFavColor myFavColor = new MyFavColor();
- WriteLine("My color preferences are :");
- for (int i = 0; i < 5; i++)
- {
- WriteLine($"{(i+1).Ordinalize()} : {myFavColor[i]}");
- }
- }
- }
- class MyFavColor
- {
- private Color[] favColor = new Color[] {Red, Green, Blue,Yellow, Orange};
- public string this[int index] => favColor[index].Name;
- }
- }



Bhavesh JadavPosted Aug 30, 2018, 8:37 AM
Great article sir........
Bhavesh JadavPosted Aug 30, 2018, 8:35 AM
Hello Sir,What is the output of the below line? WriteLine($"Age: {emp.Age}, Salary : {emp.Salary}"); Is it right below output? Age:26, Salary : 10000