In C# 6 Indexes can be initialized easily with neat & clean code,
- var user = new User
- {
- [1] = "bnarayan...@gmail.com",
- [2] = "bnarayan...@outlook.com",
- [3] = "bnarayan...@yahoo.com",
- [4] = "bnarayan...@hotmail.com",
- [5] = "bnarayan...@live.com",
- UserName = "BNarayan",
- ContactNumber= "99xxxxxxxx"
- };
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] = "bnarayan...@gmail.com",
- [2] = "bnarayan...@outlook.com",
- [3] = "bnarayan...@yahoo.com",
- [4] = "bnarayan...@hotmail.com",
- [5] = "bnarayan...@live.com",
- UserName = "BNarayan",
- ContactNumber= "99xxxxxxxx"
- };
- }
- }
- }