This question already has answers here:
All possible array initialization syntaxes
(19 answers)
Closed 2 years ago.
I have a string representation of the Guid,I'm doing some testing and I'm hard-coding some GUIDs into the code. The below way can assign Guid of single value.
Guid g = new Guid("11223344-5566-7788-99AA-BBCCDDEEFF00");
Now, My question is how i can assign values to an array list of Guid[]?
You can use the below code snippet to assign values to an array list of Guid[]
Guid[] arr = new Guid[4];
arr.SetValue(new Guid("11223344-5566-7788-99AA-BBCCDDEEFF00"), 0);
arr.SetValue(new Guid("26a285e5-5435-4daa-8d36-3186bd441cff"), 1);
arr.SetValue(new Guid("6d850084-d55a-45a9-9e83-5adf22cfbf04"), 2);
arr.SetValue(new Guid("ec43e526-51c0-41bc-b04a-2f2e72c894cc"), 3);
You can use the below code snippet to fill out your Guid array with randomly generated Guids.
Guid[] arr = new Guid[10];
for (int i = 0; i < 10; i++)
{
arr.SetValue(Guid.NewGuid(), i);
}
Related
This question already has answers here:
How to add items to a jagged array?
(2 answers)
Closed 8 months ago.
I'm trying to get some data and add those into the 2dimensional string array using foreach loop.
I made an integer variable for index reference but it keeps generating this exception error : 'Object reference not set to an instance of an object.'
can anyone give some solution for this?
the j.ID needs to be in the first place of every cell which contains three items.
string[][] pln_arr = new string[20][];
public int cnt = 0;
foreach (var j in sss)
{
MessageBox.Show("dddddddd");
pln_arr[cnt][0] = j.Id;
MessageBox.Show(pln_arr[0][0]);
cnt++;
}
You have to instantiate the inner array present in pln_arr. We can't directly assign value like pln_arr[cnt][0] = j.Id;
foreach (var j in sss)
{
MessageBox.Show("dddddddd");
pln_arr[cnt] = new string[1]; //This was missing.
//^^^^^ Update size of inner array as per your need.
pln_arr[cnt][0] = j.Id;
MessageBox.Show(pln_arr[0][0]);
cnt++;
}
This question already has answers here:
Can someone help me assign multiple string arrays into one 2d string array?
(3 answers)
Closed 2 years ago.
I have a 2d array group
float[,] group = new float [2,3];
My understanding is that, after this line of code I have 2 3-elements arrays in group.
{{3elements},{3elements}}
I also have member
float[] member = new float[3] {1,2,3};
I want to set the first array inside the 2d array "group" to match "member" so that I can have {{1,2,3},{}}
I tried group[0] = member;
But I got an error. What's the correct way of doing this?
You can try this
float[,] group = new float[2, 3];
float[] member = new float[3] { 1, 2, 3 };
for(int i = 0; i < member.Length; i++)
{
group[0, i] = member[i];
}
This question already has answers here:
Does C# support a variable number of arguments, and how?
(4 answers)
Closed 5 years ago.
I'm trying to pass a variable number of arguments here.
Codification codebook = new codification(data,"Attr1","Attr2",..."AttrnumOfAttrColumns,"Result")
and also here:
int[][] inputs = symbols.ToJagged<int>("Attr1","Arrt2",..."AttrnumOfAttrColumns);
I was trying to do it with a for loop which is not the right.Is there a way to do this?
Codification codebook = new Codification(data, for (int i = 0; i < numOfAttrColumns; i++) {return "Attr"+Convert.ToString(i) }, "Result");
According to Accord.net manual
You can put
Codification codebook = new codification(data, new string[] {
"Attr1", "Attr2", ..., "AttrnumOfAttrColumns", "Result"});
This question already has answers here:
How to populate/instantiate a C# array with a single value?
(26 answers)
Closed 6 years ago.
So lets say we have an array of doubles like this one that is later used for other stuff.
double[] myArray = new double[25];
How would I go about replacing all the values in that array with a set value?
There are flashier ways, but
for (int i = 0; i < myArray.Length; ++i){
myArray[i] = foo; /*the new value*/
}
is clear and simple.
The "flashy" way (simply create a new array):
var array = Enumerable.Repeat(value, count).ToArray();
Or
Array.ConvertAll(array, e => value);
This question already has answers here:
Best way to randomize an array with .NET
(19 answers)
Closed 6 years ago.
I have a c# int array that contains numbers from 1 to 100
that means that
myArray[0] = 1;
myArray[1] = 2;
....
myArray[99] = 100;
But I want to rearrange them in this array randomly, is it possible in c# ?
Using Random and Linq, you can do it easily:
Random r = new Random();
myArray = myArray.OrderBy(x => r.Next()).ToArray();
The above provides a random sort order for each element in the array using a Random object.
You need to add using System.Linq; at the top of your file in order to use OrderBy extension method.