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 5 years ago.
Improve this question
calling method in foreach loop like
foreach (object ofr in DataHelper.GetOffers(id, object))
is better
or take this DataHelper.GetOffers(id, object) out and assign that to variable like foreach (object ofr in variableObject) which is the better way and which will perform better and why?
Thanks
Neither is better and both will perform the same.
It's a matter of personal style preference.
IL code will be optimized in both ways in the release. So, either way, it will be the same in terms of performance.
In both cases you are only calling DataHelper.GetOffers(id, object) once. Thus there are no performance implications either way.
When people talk about moving methods calls out from loops they mean the loop's inner scope or the loop's body. Because if it's a call that can be made only once, you are wasting time by doing it in every loop's repetition.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am trying to make an extension method in C# that takes in a collection of objects and can make a hash from it. The problem is I haven't been able to figure out a fast way of doing it. I am trying to have it be done in O(1) time but I haven't found any useful information out there. This is what I currently have but it is slow and doesn't really work (its just an example):
public static int GetCollectionHash(this IEnumerable collection)
{
HashCode hash = new();
foreach (var o in collection)
{
hash.Add(o);
}
return hash.ToHashCode();
}
the only way to do what you want is to create your own collection and overriding the add/remove/etc method and update a private variable that contain the hash when they are called
at that point, just read that new variable
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 3 years ago.
Improve this question
void MyFunc(int var)
{
// Some Code
}
void MyFunc(List<int> varList)
{
// Some Code
}
What is the performance of parameter passing to these two functions?
There answer is, there should be little difference.
The first is allocating an int and copying the value type of a int,
The second is allocating an reference and copying a reference (which for all intents-and-purposes is an uint / ulong)
There is no appreciable difference.
However, the bigger problem is why you are care about these micro-optimisations, i think you are over thinking this. You can always test this for your self. Either look at the jitted asm, or download BenchmarkDotNet and run a performance test
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 5 years ago.
Improve this question
I'm trying to improve my MVC.NET Core-fu and I ended up in a bunch of methods in all my controllers looking like this (note the outermost, general, repetitive try-catch).
[HttpPost]
public IActionResult DoSomething([FromBody] Thing thing)
{
try
{
if (...)
return Ok(thing);
else
return NotFound();
}
catch (Exception) { return BadRequest(); }
}
The way I see, I want to have the baddy-requesty just in case. Now, that adds a lot of code to otherwise rather simple controller and I have a suspicion that someone before has thought of this and proposed a solution.
I haven't found such thing (and I was informed that filters are obsolete for usage in Core). Possibly it's because I'm not familiar with the right search keys.
This seems excessive to me. Many methods won't execute instructions that are likely to fail. For those extremely rare cases, a global error handler would be more than sufficient.
For those methods that access the disk, a database, or perform calculations that could raise exceptions (something you should probably be avoiding in the first place), a try...catch block makes sense.
But even in these cases, if a method with a try...catch handler calls another method, there is no reason to put handlers in that other method.
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
There are times I instantiate a new object within a method call for sake of streamlining code instead of assigning the new object to a variable. What drawbacks exist in doing one or the other?
T myobj = new T();
elements.Add(myobj);
--vs--
elements.Add(new T());
Need a reference later
As adaam mentioned in the comments, if you need to keep a reference to an object because you'll be using it, then it's better to do it this way.
T myobj = new T();
elements.Add(myobj);
T.DoStuff(); //this might need to happen further down in the code, so keeping the reference is handy. Otherwise we'd have to dig it out of the elements. And you might be thinking "well, I don't need to reference it later in the code." But what if you're refactoring the code and it requires some modification? Now you'll need to change it, rather than having done it with a separate declaration in the first place.
Debugging
A common situation is when you're stepping through code with the debugger. It's difficult to see properties of an object that was created in this manner.
elements.Add(new T());
When given its own reference, you can easily use your IDE's debugging tools to check the values if the code is written like this:
T myobj = new T();
elements.Add(myobj);
Readability
Another reason to choose one over the other would be readability. That one is opinion based, but you should ask the team you're working with which is more readable in order to determine which practice to follow. Asking everyone on Stack Overflow which reads better is off topic.
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
Ill start with an example or two. Take the following sample class:
class Sample
{
private object _someObject;
public Sample(object someobject)
{
_someObject = someobject;
// If I then wanted to pass someobject to a method within the constructor,
// is it better to use the field version or the parameter version. Example:
SomeMethod(someobject);
// OR
SomeMethod(_someObject);
}
}
Additionally, I have just finished the book titled "Efficient C#" by Bill Wagner and would like to know if there are any more books out there with a similar format as this one.
I am interested in knowing why I should write code the way it is written (More efficient IL for example)
Thanks in advance guys :)
It makes no difference, they're all references to the same object.