Want to become a Vibe Coder? Join Vibe Coding Training here
x
C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Index Initializers in C# 6
WhatsApp
Banketeshvar Narayan
9y
7.9
k
0
1
25
Blog
In C# 6 Indexes can be initialized easily with neat & clean code,
var user =
new
User
{
[1] =
"
[email protected]
"
,
[2] =
"
[email protected]
"
,
[3] =
"
[email protected]
"
,
[4] =
"
[email protected]
"
,
[5] =
"
[email protected]
"
,
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] =
"
[email protected]
"
,
[2] =
"
[email protected]
"
,
[3] =
"
[email protected]
"
,
[4] =
"
[email protected]
"
,
[5] =
"
[email protected]
"
,
UserName =
"BNarayan"
,
ContactNumber=
"99xxxxxxxx"
};
}
}
}
Index Initializers
C# 6
People also reading
Membership not found