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.
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 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 been searching for ages about IEnumerable online but they are all in the program.cs file.
Can anyone please tell me what is it at the below code?
namespace customBank.Interfaces
{
public class Bank : customBank // the bank take the format of customBank as interface.
{
public IEnumerable<IStatementRow> GetMiniStatement(IAccount account)
}
}
IEnumerable is a list of "things" that you can loop through. In this situation, it is a list of IStatementRow things.
A list of IStatementRow things is returned when the GetMiniStatement function is called.
See: IEnumerable Interface
Also see: Interfaces
Once you read that you will understand pretty well whats happening on your code.
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
This is basically a a design question:
I am rewriting an application in C# which is basically written in C++. C++ has this nice concept of Header files which will gold a lot of declared constant values for the consuming file.
However, we do not have Header files in C#. I may have two options
Create a class which will hold a lot of constant values for me(No so standard)
Store values in XML (Standard-But involves a lot of parsing hassle)
Which is a better solution? Is there any other solution that I may not know of?
Personally i'd use a static class and place all the values in there.
public static class Constants
{
public const int Ten = 10;
public const int Twenty = 20;
....
}
EDIT
As #JonSkeet suggested, it's better if you store these values in classes they pertain to, however, that might not always be possible.
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 9 years ago.
Improve this question
Is there any naming convention for "utility" methods whose only reason of existence is to make your Linq statements clearer?
Example:
List<Member> myList = GetMembers();
myList.Where(AllMembersAreBlue);
//lots of code...
public bool AllMembersAreBlue(Member member)
{
//code
}
What would be the most correct way to name methods like the mentioned above (AllMembersAreBlue) ? Is there any convention?
I did some googling and found no answers.
As long as you understand the intent I'd say use whatever name suits you.
In this case I don't really see the point of a separate method, though:
myList.Where(m => m.Members.All(x => x.Color == Color.Blue))