How to set mulitple records in constructor


I'm not using this in a real app but I was just curious on how to do this (C#).

I set one record of sample data in the constructor :

public class MikesClass { public MikesClass() { Id = 01; Name = "Mike"; } public int Id { get; set; } public string Name { get; set; } }

but I'm confused on how to set another record in it :

public MikesClass() { Id = 01; Name = "Mike"; Id = 02; Name = "Tom"; ??? }

If possible to do this, what is the syntax? thanks


You completely misunderstood what a constructor is. A constructor is for one single object. It creates one single object. Thus you cannot set another record with it. That record will be a different object. You just set the values as arguments to constructor when you create another record.

So, should at least be like this -

public class MikesClass { public MikesClass(int id, string name) { Id = id; Name = name; } public int Id { get; set; } public string Name { get; set; } }

and in some distance place when creating multiple records/objects -

var m1 = new MikesClass(0,"name1"); var m2 = new MikesClass(1, "name2");


Using the code you specified above, each time you write:

MikesClass mc = new MikesClass();

you will get an object of type MikesClass with the Id property set to 1 and the Name property set to "Mike". Since each instance of MikesClass represents a single object, you cannot have multiple objects represented within it.

What you can do though, is modify your constructor to take the two values as parameters. Like this:

public MikesClass(int id, string name) { Id = id; Name = name; }

You can then use this code to create multiple MikesClass objects like so:

MikesClass mike = new MikesClass(1, "Mike"); MikesClass tom = new MikesClass(2, "Tom");

Hope this makes sense.


What you're showing is a constructor. It is run when you create an instance of the MikeClass class. What you want is to create several instances. Maybe in an array?

MikeClass[] array = new MikeClass[2]; MikeClass mc = new MikeClass(); /first instance mc.Id = 1; mc.Name = "Mike"; array[0] = mc; mc = new MikeClass(); //another instance mc.Id = 2; mc.Name = "Tom"; array[1] = mc; };

This is using object initializer syntax:

MikeClass[] array = new MikeClass[] { new MikeClass { Id = 1, Name = "Mike" }, //first instance new MikeClass { Id = 2, Name = "Tom" } //another instance };

You can also create a constructor for the MikeClass class that takes parameters:

public MikeClass(int id, string name) { Id = id; Name = name; }


MikeClass[] array = new MikeClass[] { new MikeClass(1, "Mike"), new MikeClass(2, "Tom") };


  • Applying synsets to pandas
  • spacy module install in conda
  • Error Handling in System.Net.HttpWebRequest::GetResponse()
  • Tkinter Grid Columnspan ignored
  • Generate c# object code and assign values to its properties from an xml document
  • R add index column to data frame based on row values
  • What are the best practices for migrating an Oracle 10g database to Microsoft SQL 2008 R2? Applicati
  • xquery to return the element name
  • How can I determine the author of a block of code programmatically in TFS?
  • How can I get rid of dynamic SQL
  • MATLAB - Classification output
  • How to replace TouchesBegan with UIGestureRecognizer
  • Stitching 2 images (OpenCV)
  • What's the name of this finding square root algorithm?
  • ZipList with Scalaz
  • Alamofire and Reachability.swift not working on xCode8-beta5
  • Is it possible to define rest argument in OCaml?
  • What Makes These Two Array Adds Different?
  • cordova is not defined - cordova.js has already been loaded :: Ionic
  • Find group of records that match multiple values
  • Bigquery event streaming and table creation
  • Cast between interfaces whose interface signatures are same
  • Question about instantiating object
  • Cannot upload to OneDrive using the new SDK
  • RxJava debounce by arbitrary value
  • How does this usort cmp function actually work?
  • one Local Olampyad Questions on Informatic in 2011
  • Admob requires api-13 or later can I not deploy on old API-8 phones?
  • Email verification using google app script and google forms
  • How to avoid particles glitching together in an elastic particle collision simulator?
  • Change Inet root folder for iis 7
  • Java Scanner input dilemma. Automatically inputs without allowing user to type
  • QLineEdit password safety
  • Sails.js/waterline: Executing waterline queries in toJSON function of a model?
  • Redux, normalised entities and lodash merge
  • Do create extension work in single-user mode in postgres?
  • AT Commands to Send SMS not working in Windows 8.1
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • How do I configure my settings file to work with unit tests?
  • Is it possible to post an object from jquery to bottle.py?