Home > Back-end >  How to assign value in generic list without using a for loop
How to assign value in generic list without using a for loop

Time:12-11

I have an generic List with 7 fields and 15 rows. I would like to assign some default values to field1 and field2 without using a for loop. Please advise on how to do this.

public class TestSetup: BaseModel
{
   public int TestID { get; set; }
   public int StyleStockID { get; set; }
   public int? TypeID { get; set; }
   public int? PartNoID { get; set; }
   public int? ComponentID { get; set; }
   public int? LeatherID { get; set; }
   public int? ColorID { get; set; }          
}
    
protected List<StyleBomLeatherSetup> styleBomLeatherSetups { get; set; } = new List<StyleBomLeatherSetup>();

CodePudding user response:

Just set default value as

public int? TypeID { get; set; } = 10;

CodePudding user response:

Why would you need a "for loop"? You can just slap an = on your properties and assign them a default value.

public class TestSetup : BaseModel
{
   public int TestID { get; set; } = 1000; // default value
   public int StyleStockID { get; set; } = 25; // default value
   public int? TypeID { get; set; }
   public int? PartNoID { get; set; }
   public int? ComponentID { get; set; }
   public int? LeatherID { get; set; }
   public int? ColorID { get; set; }
}

EDIT

You said without for loop, but you never said anything about a foreach loop, so consider an approach similar to this:

using System.Reflection;

TestSetup testSetup = new TestSetup();
Type type = testSetup.GetType();
PropertyInfo[] properties = type.GetProperties();

foreach (PropertyInfo property in properties)
{
     if (property.PropertyType == typeof(int)) property.SetValue(testSetup, 1000);
}

Or if you want to set them individually you can do something like:

if (property.Name == "TestID") property.SetValue(testSetup, 1000);

CodePudding user response:

Not sure I understood your question

If you are looking for a way to populate the list without a loop, you may use an initializer like in the following example

using System;
using System.Collections.Generic;

namespace Test
{
    class Program
    {
        class Person
        {
            public string Name { get; set; }
            public DateTime Born { get; set; }
        }

        static void Main()
        {
            List<Person> people = new List<Person>
            {
                new Person { Name = "Fabio", Born = new DateTime(1975, 01, 23) },
                new Person { Name = "Laura", Born = new DateTime(1985, 07, 07) },
            };

            Console.WriteLine(people.Count);
        }

    }
}

EDIT

Based on your comment, I realize I misunderstood your question

If you need to set the properties to a default value, then I cannot think of a better solution than the one that has already been suggested in the other answers

public string Name { get; set; } = "Fabio";
public DateTime Born { get; set; } = new DateTime(1975, 01, 23);

Fabio

  •  Tags:  
  • c#
  • Related