Inititalize array in C#

Maybe this post is not necessary, but I still have thought that beginners will struggle with array initialization and C# objects.

Here we go with an Int array first,

Syntax: Type [] <variable>;

I am combining declaration with an assignment of values as follows

 int[] x = new int[] { 1, 2 };

Object Array

What about object array? Let’s have a look at the Account Class

class Accounts
 public Accounts()
 // TODO: Complete member initialization

This is a blank class with only one default constructor. We are going to create an array of Accounts with size 2.

 Accounts[] ac = new Accounts [] { new Accounts( ),
 new Accounts( ) 

Here in all these arrays, we have explored the size will be automatically determined while we initialize the values dynamically to some extent.

Published by


A developer,teacher and a blogger obsessed with Python,Dart and open source world from India

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.