Filling an array of int at declaration [duplicate] - c#

This question already has answers here:
Direct array initialization with a constant value
(6 answers)
Fastest way to fill an array with a single value [duplicate]
(3 answers)
Closed 8 years ago.
I'd like to declare an array of int rows with a variable size (X) and init all values to 1. For the moment I use this :
int[] rows = new int[X];
for (int i = 0; i < rows.Length; i++)
{
rows[i] = 1;
}
Is there any faster/shorter way to do it with some sort of fill(1) or int[] rows = new int[X] {1}; ?

LINQ:
int[] rows = Enumerable.Repeat(element:1, count: X).ToArray();// named parameter - X
// doesn't tell anything

Related

resize array in for loop in c# [duplicate]

This question already has answers here:
change array size
(15 answers)
Closed 3 months ago.
I need to run this loop and add the values to the length array each time the loop iterates, so far each time i loop the temp array is cleared so the data is getting lost each iteration. I want to resize the array and add the user input to the length array each iteration.
int[] lengthArray = new int [1];
for (int i = 0; i < lengthArray.Length; i++)
{
lengthArray[i] = int.Parse(Console.ReadLine());
int[] temp = new int [lengthArray.Length + 1] ;
temp[i] = lengthArray[i];
lengthArray = temp;
}
You probably want to store your result in a List<int> instead of an array int[] .
Then you each time you want to add an element in the lengthArray you can just call the Add method.

Is it possible to concreate variable in c# [duplicate]

This question already has answers here:
C# dynamically set property [duplicate]
(5 answers)
Closed 1 year ago.
I have some variables and there results which are going into database.
For example
float[] qty1;
Quote quote = new Quote();
quote.qty1 = qty1[0];
quote.qty2 = qty1[1];
quote.qty3 = qty1[2];
quote.qty4 = qty1[3];
I try to make this process more dynamic
for (int i = 0; i <= 3; i++)
{
quote.qtyi = qty1[i];
}
please help me how i can use quote.qtyi and value of i, so it will read quote.qty1, quote.qty2, quote.qty3, quote.qty4
Use reflection:
for (int i = 0; i <= 3; i++)
{
quote.GetType().GetProperty("qty"+i).SetValue(quote,qt1[i]);
}
quote.getType().getProperty("qty"+i) -> Gets the property called "qty"+i from the object "quote".
.setValue(quote,qt1[i]) -> sets the value "qt1[i]" to the property (if she exists) of the object "quote".
My first answer had TYPOS, now it's compilable

Iterating through variable names with for loop [duplicate]

This question already has answers here:
Variables in a loop
(9 answers)
Loop through object variables with different number on the name [duplicate]
(5 answers)
Iteration with variable name [duplicate]
(1 answer)
Closed 2 years ago.
I am trying to use a for loop to iterate through a series of variable names each ending in a number from 1 to 10. I have seen a few other answers to this question but have been unable to make any work for my specific situation. My code is as follows:
string cat2Pos0 = cat2[0];
int numOfPos0 = cat2.Where(x => x.Equals(cat2Pos0)).Count();
List<int> indexOfPos0 = new List<int>();
bool check = cat2.Contains(cat2Pos0);
int index = 0;
if (check == true)
{
for (int i = 0; i < numOfPos0; i++)
{
index = cat2.FindIndex(x => x == cat2Pos0);
indexOfPos0.Add(cat2.IndexOf(cat2Pos0));
}
}
else if (cat2Pos0 == "-")
{
numOfPos0 = 17;
}
I need to loop through 10 variables names cat1 - cat10. In the code: whenever there is the phrase "cat" I need to be able to adjust it depending on a for loop e.g. cat1 or cat5:
string cat3pos0 = cat3[0];
or:
index = cat3.FindIndex(x => x == cat3Pos0);
Unfortuantely, I am unable to simply write out each variation individually as that would use up almost 3700 lines of code and I was hoping that there would be a better way of achieveing this.
Many thanks, all help is greatly appreciated,
Josh
See here how to use reflection for this. (something like this.GetType().GetField("cat" + i.ToString());.)
But I would really suggest changing your variables to one array of 10 variables. So cat will be an array of arrays (since your cat's seem to be arrays).

C# - Field values of an array picking up values of a loop [duplicate]

This question already has answers here:
How to get first N elements of a list in C#?
(7 answers)
How do I clone a range of array elements to a new array?
(26 answers)
Closed 5 years ago.
I'm training C# on a simple card game. I have methods that shuffle and deals the cards. I have a random deck that is well generated.
Is it possible to set an array for the player1 cards, picking up the first ten values of the array.
Here is a part of my code :
currentCard = 0;
public Card DealCard()
{
if (currentCard < deck.Length)
return deck[currentCard++];
else
return null;
}
I want to pick up for example ten first values of
deck[currentCard++]
Any suggestions will be appreciated, thanks for your help !
You mean you want to pull the first 10 enties into another array? Something like;
var player1Cards = deck.Take(10);
or
List<int> player1Cards = new List<int>();
for (int i = 0; i < 10; i++){
player1Cards.Add(deck[i]);
}

C# cannot generate a random string [duplicate]

This question already has answers here:
Random.Next returns always the same values [duplicate]
(4 answers)
Random number generator only generating one random number
(15 answers)
Closed 5 years ago.
I cannot work out why C# is doing this.
Here's my code;
private string RandomString(int length)
{
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
string randomString = "";
for(int i = 0; i < length; i++)
{
randomString += chars.ToCharArray()[new Random().Next(chars.ToCharArray().Length)];
}
return randomString;
}
First result:
"wwwwwwwwwwwwwwwwwwww"
Second result:
"ssssssssssssssssssss"
Third result:
"mmmmmmmmmmmmmmmmmmmm"
When you generate a random number generator using new Random(), its seed will be based on the current time, so it will end up being the same thing for each iteration of the loop, since execution will be fast. Instead, you want a var rng = new Random() outside of the loop, and use rng.Next inside the loop.

Categories

Resources