In C# 6 Indexes can be initialized easily with neat & clean code,
Complete Code
- namespace IndexInitializersCSharp6
- {
-
- class User
- {
- public string UserName { get; set; }
- private string[] EmailIds = new string[10];
- public string ContactNumber { get; set; }
- public string this[int index]
- {
- set { EmailIds[index] = value; }
- get { return EmailIds[index]; }
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- var user = new User
- {
- [1] = "[email protected]",
- [2] = "[email protected]",
- [3] = "[email protected]",
- [4] = "[email protected]",
- [5] = "[email protected]",
- UserName = "BNarayan",
- ContactNumber= "99xxxxxxxx"
- };
- }
- }
- }