Why is my function passing 10 as a parameter? [duplicate] - c#

This question already has answers here:
Captured variable in a loop in C#
(10 answers)
Thread parameters being changed
(2 answers)
Closed 2 years ago.
I would like to add onClick listeners to my buttons in Unity, C#.
I have an array of these buttons. I am trying to loop through that array, and for each button, I am adding an onClick listener which calls a function that takes an index parameter.
In that function, I print out the paramater that the function has received.
However, when I click on one of these buttons, I get 10 as an output every time.
for(int i = 0; i < roleButtons.Length; ++i) {
roleButtons[i].onClick.AddListener(delegate { changeEquipment(i); });
}
void changeEquipment(int index) {
Debug.Log(index); //Here I got 10 as an output after every single click.
}

Related

C# - continuously run a while loop until key is pressed? [duplicate]

This question already has answers here:
How to read/write on console at the same time using threads
(2 answers)
How do I "Pause" a console application when the user presses escape?
(2 answers)
Closed 1 year ago.
I am currently working on a school project that requires data to be generated, processed and stored every 2 seconds. For that, I am using while(true) loop. The problem I have is allowing user to break that loop and request generated data. Generating/storing has to be a continuous process that happens in the background, so I can't ask user for input at the beginning of each iteration. I hope someone could help me out. This would be a simple example:
int i = 0;
List<int> list = new List<int>();
//Console.WriteLine("Generating will continue until any key is pressed... ");
while (true)
{
i++;
li.Add(i);
Thread.Sleep(2000);
}
foreach (int i in list)
{
Console.Writeline(i);
}
while(!Console.KeyAvailable)
{
//do work
}

How do I delete a specific list of lines from a text document? [duplicate]

This question already has answers here:
How can I efficiently remove elements by index from a very large list?
(6 answers)
Remove oldest n Items from List using C#
(6 answers)
Remove all but the first item in a list
(2 answers)
How to remove selected array values in C#
(5 answers)
Move First 10 List Items to Another Item List
(5 answers)
Closed 1 year ago.
This is what I have but there isn't anything that talks about how to delete multiple specific lines, for example, from line 2 - 8.
var file = new List<string>(System.IO.File.ReadAllLines(lsbMatches.SelectedItem.ToString()));//location
file.RemoveAt(2);// i want to change this to multiple lines
File.WriteAllLines(lsbMatches.SelectedItem.ToString(), file.ToArray());//location
You can use RemoveRange because you have converted your text file into a list of strings.
https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.removerange?view=net-5.0
You're best option is to do :
file.RemoveRange(2, 6);

How can I find the smallest word in an array for a form application? [duplicate]

This question already has answers here:
C# finding the shortest and longest word in a array
(9 answers)
Closed 5 years ago.
I am making a program that takes in lines of user input and finds the largest word in that list. I have everything set up but that last bit and am not sure where to even start. I think I need to make a function and then call it to display it in a text box.
If you have an array, for ex: string[] initArray={"one","twoo","threeeee"};
then
public string ReturnSmalles(string[] initArray)
{
return initArray.OrderBy(n => n.Length).FirstOrDefault();
}

how to measure the instruction count during execution? [duplicate]

This question already has answers here:
Is there a way to count the number of IL instructions executed?
(5 answers)
Closed 7 years ago.
I want to mesure how much instruction are executed in method example.
int a=0;
functionTest();
a=getCountInstruction() // return the number of instruction executed until now.
Their any way to do it using Profiler or some Classes ?
You have to add increase counter when you invoke your method
static int a=0;
functionTest()
{
a++;
//your code
}

.NET list access index [duplicate]

This question already has answers here:
Is the order of elements on a C# List<T> deterministic?
(3 answers)
Closed 8 years ago.
Follow up question from How to select List<> by its index and return the content?
Can someone please comment if the elements in the list are expected to retain order? In the example above, is "Belly Buster" always expected to be at index 1 or are there cases where that might not be true?
List<string> pizzas = new List<string>();
pizzas.Add("Angus Steakhouse");
pizzas.Add("Belly Buster");
pizzas.Add("Pizza Bianca");
pizzas.Add("Classic Cheese");
pizzas.Add("Friday Special");
string result = pizzas[1]; // result is equal to "Belly Buster"
index starts at 0 so yes if the code stays the same..belly buster will always be at 1

Categories

Resources