Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
While looking through some dll's with ILSpy I came across the following code:
void RenderFiles(List<List<string>> pdfFiles)
{
int num;
for (int i = 0; i < pdfFiles.Count; i = num + 1)
{
// ....
num = i;
}
}
The introduction of the num variable seems strange to me. Why would the compiler introduce an extra local variable?
The original code is just a simple loop, although it uses a count variable and not a foreach enumerator:
void RenderFiles(List<List<string>> pdfFiles)
{
for (int i = 0; i < pdfFiles.Count; i++)
{
}
}
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I've been looking up a lot of videos, tried using the bubble sort method, insert sort method, but nothing seems to work for this particular problem. I am supposed to add a string (movie name) to an array, but I must do it alphabetically. I can not sort the array after it's completed, it must be done while I add the new strings.
I've seen a lot posts with similar questions like this but all of them sort the array after its completed!
Here is a couple of methods that might help you.
private void PrintAlphabetically()
{
string[] movies = new string[5];
movies[0] = "b";
movies[1] = "x";
movies[2] = "m";
movies[3] = "a";
movies[4] = "t";
AddToStringArray(ref movies, "s");
Array.Sort(movies, (x, y) => String.Compare(x , y));
for (int i = 0; i < movies.Length; i++)
{
Console.WriteLine(movies[i]);
}
}
private void AddToStringArray(ref string[] array, string item)
{
List<string> list = array.OfType<string>().ToList();
list.Add(item);
array = list.ToArray();
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
hello guys i am new to c# programming but i was just try to take an input from the user by c# console application and save that info to an string array but its giving me an error
Error 1 No overload for method 'ReadLine' takes 1 arguments
namespace demo_try
{
class Program
{
static void Main(string[] args)
{
//string[] sarry ={"hi -", "me"};
//for (int i = 0; i < sarry.Length; i++)
//{
// Console.Write("- {0} -", sarry[i]);
//}
//foreach (var n in sarry)
//{
// Console.Write("-{0}",n);
//}
string [] sarray= new string [10] ;
for (int i = 0; i < sarray.Length; i++)
{
Console.Write("Enter the values for an array {0}", sarray[i]);
Console.ReadLine(sarray[i]);
}
}
}
}
Error 1 No overload for method 'ReadLine' takes 1 argumentts
that would be great if you guys help me out in this matter :)
The error is pretty self explanatory. Read this documentation for reference https://msdn.microsoft.com/en-us/library/system.console.readline%28v=vs.110%29.aspx
You need to change your code to
sarray[i] = Console.ReadLine();
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am going Round-Robin Algorithm. I am doing in that everything must be created dynamically and Randomly. When I want to go second time to check if I have and value the is more than 0 in the checking point it says that "The given key was not present in the dictionary". How can I Fix this problem.
private int GetNextNodeToProcesssRandom()
{
int NextNodeIndex = -1;
if (NextNodeToProcess >= this.waytosave.Count)
{
NextNodeToProcess = 0;
}
for (int i = NextNodeToProcess; i < this.waytosave.Count; i++)
{
if (this.waytosave[i] > 0)//the problem appears here when the cod goes for the second time.
{
NextNodeIndex = i;
break;
}
}
NextNodeToProcess++;
return NextNodeIndex;
}
It is somehow unclear what is your exact goal. However if you want to loop through a Dict you can use:
foreach(KeyValuePair<TKey, TValue> entry in MyDict)
{
// do something with entry.Value or entry.Key
}
Now if you want to add a check in your program you can try:
if (MyDict.ContainsKey(this.waytosave[i]))
///continue execution...
A look here might help you
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have the following for loop written in PHP:
$activesFilters = 0;
foreach ($_GET as $key => $value) {
if(!empty($value)) $activesFilters++;
}
if(isset($_GET['Filter'])) { $activesFilters = $activesFilters - 1; }
if(isset($_GET['CustomFilter'])) { $activesFilters = $activesFilters - 1; }
if(isset($_GET['showArchived'])) { $activesFilters = $activesFilters - 1; }
and I want to convert it over to ASP.NET C# Razor.
So far I have:
int activeFilter = 0;
#foreach(Request.QueryString as key in value) {
if(!empty(value)) activeFilters+1;
}
#if(Request.QueryString['Filter']) activeFilters = activeFilters - 1;
#if(Request.QueryString['CustomFilter']) activeFilters = activeFilters - 1;
#if(Request.QueryString['showArchived']) activeFilters = activeFilters - 1;
It's the foreach loop I'm confused about.
As I don't understand how to handle the key => value in C#
I've written the above based on: http://msdn.microsoft.com/en-us/library/ttw7t8t6(v=vs.90).aspx but it doesn't show any examples for handling the key + value part.
UPDATE:
Based on some help from below:
#int activeFilters = 0;
#foreach (string key in Request.QueryString)
{
string value = Request.QueryString[key];
if(String.IsNullOrEmpty(value)){
return activeFilters+1;
}
}
However it says activeFilters does not exist in the current context. How do I expose it to the foreach loop in C#? As this is fine in the PHP world.
foreach (string key in Request.QueryString)
{
string value = Request.QueryString[key];
// add your other code here
}
Now, you iterate over all keys in the query string, and then you get their values using Request.QueryString[key].
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Not sure if this is a bit of a superfluous question, but consider I have these methods:
void Foo(SomeClass x)
{
//Some code
}
void Foo(AnotherClass x)
{
//Some code
}
And let's say that I want to call a specific overload (the SomeClass one) with null, here are my options:
Foo((SomeClass)null)
Foo(null as SomeClass)
Foo(default(SomeClass))
Basically, which is the best to go for? Are there any significant performance differences between the different approaches? Is a specific way generally considered more 'elegant' than the others?
Thanks
Option 4: create another overload:
void Foo()
Calling with an explicit null that you need to cast? Umm...eww...
To "officially" answer your question. Try it!
var sw = Stopwatch.StartNew();
for (int i = 0; i < 1000000; i++) {
Foo(null as string);
}
Console.WriteLine(sw.ElapsedMilliseconds);
sw = Stopwatch.StartNew();
for (int i = 0; i < 1000000; i++) {
Foo((string)null);
}
Console.WriteLine(sw.ElapsedMilliseconds);
sw = Stopwatch.StartNew();
for (int i = 0; i < 1000000; i++) {
Foo(default(string));
}
Console.WriteLine(sw.ElapsedMilliseconds);
Console.ReadLine();
I got ~4ms for all 3 approaches.
When I open up the program in reflector, I see that all of the calls have been turned into: Foo((string) null);
So, you can choose whatever you find most readable. The IL all ends up exactly the same.