Calling an overload with null: casting vs default [closed] - c#

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.

Related

How to insert a string in alphabetical order to a new array? [closed]

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();
}

I want my if loop to do nothing if certain conditions are met [closed]

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 6 years ago.
Improve this question
i want my if loop to do nothing when the conditions that i gave it are there
i'm new to C# and winform so i searched the internet but didnt find an answer that seems to work and right now i have no idea what to do.
,Mo
screenshot of the loop
If I understand your question correctly, you want to "cancel" all operations in the current method, right? You can use return; to do that:
if(value2 == null) return;
There is just one other thing wrong with your code: value2 will never be null.
decimal value2;
if(!decimal.TryParse(result[1], out value2)) return;
should work a lot better ;)
I can't see any loop in the code provided. You want to change Parse to TryParse and use return in order to return from the method (== do nothing):
public void button14_Click(Object sender, EventArgs e) {
string[] result = input1.Text.Split(Oprator);
//TODO: it may appear, that you want TryParse here as well
decimal value1 = decimal.Parse(result[0]);
decimal value2;
// If you have too few items, and thus you have no "value2" - do nothing
if (result.Length < 2)
return;
// Try parse result[1] to decimal; if parse fails (e.g. result[1] == "zzz") - do nothing
if (!decimal.TryParse(result[1], out value2))
return;
// you have value1, value2, Oprator; put required logic here
switch (Oprator) {
...
}
}
Have you thought about a "While" loop?
int n = 1;
while (n < 6)
{
Console.WriteLine("Current value of n is {0}", n);
n++;
}

Is this emitted code useful? [closed]

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++)
{
}
}

Facing this error : "No overload for method 'ReadLine' takes 1 arguments" [closed]

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();

Getting the Values from Dictionary [closed]

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

Categories

Resources