In C# 6 Indexes can be initialized easily with neat & clean code,

  1. var user = new User
  2. {
  3. [1] = "[email protected]",
  4. [2] = "[email protected]",
  5. [3] = "[email protected]",
  6. [4] = "[email protected]",
  7. [5] = "[email protected]",
  8. UserName = "BNarayan",
  9. ContactNumber= "99xxxxxxxx"
  10. };

Complete Code

  1. namespace IndexInitializersCSharp6
  2. {
  3. class User
  4. {
  5. public string UserName { get; set; }
  6. private string[] EmailIds = new string[10];
  7. public string ContactNumber { get; set; }
  8. public string this[int index]
  9. {
  10. set { EmailIds[index] = value; }
  11. get { return EmailIds[index]; }
  12. }
  13. }
  14. class Program
  15. {
  16. static void Main(string[] args)
  17. {
  18. var user = new User
  19. {
  20. [1] = "[email protected]",
  21. [2] = "[email protected]",
  22. [3] = "[email protected]",
  23. [4] = "[email protected]",
  24. [5] = "[email protected]",
  25. UserName = "BNarayan",
  26. ContactNumber= "99xxxxxxxx"
  27. };
  28. }
  29. }
  30. }