Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 6 years ago.
Improve this question
I create a new DataContract and I need to add a new field.
That field will be describe a type of export job: Ad-hoc or virtual standby.
Which type do I need to choose to represent this idea: bool or enum? Why?
Consider how it will be used as method parameter
void F(DataContract dataContract)
If it's Enum, you invoke it via
F(DataContract.AdHoc)
F(DataContract.VirtualStandby)
They are all very clear. What if it's bool:
F(bool dataContract)
Then invoke it via
F(true)
Execute me! Is it Ad Hoc or Virtually Standy? You have educate every method consumer what true/false mean in this context. Even they want to write clear code by
F(true/*Ad Hoc*/);
It's really less readable.
Related
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've got this idea from Java - i was told that you should declare a collection like this
List<Object> myList = new ArrayList<Object>();
where ArrayList is a class implementing List interface. The point of this is to enhance maintainability by generalizing code - as, should you change teh implementation to, e.g LinkedList<Object>, you could to it 100% painlessly.
So, projecting this on C#, is it considered a good practice to do the same thing in C# :
IList<Object> list = new List<Object>()
?
EDIT : i just found that LinkedList in C# does not even implement the IList interface, so i guess it settles the question for lists at least
Yes, generally this is a good practice to use interfaces wherever you can.
There are two important exceptions for containers, though:
When you must use a hash-based container for objects that are not comparable, use HashSet<T> or Dictionary<TK,TV>,
When you declare a local variable, using var for implicit typing is often preferred for convenience,
Note that if you plan to use a sorted container you have IOrderedSet<T> and IOrderedDictionary<TK,TV>.
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 7 years ago.
Improve this question
I use C#.
When I defined Hoge method below,
void Hoge(bool isBar){}
I get the Hoge method like below
var methodName = this.Hoge as Action<bool>).Method.Name;
However, I can't understand what does this.Hoge type.
Because, it can assign and casting.
but, it can't give me method name directly.
this.Hoge.Method.Name;
and, it also error. typeof(this.Hoge)
what is method variable exactly?
The code you provided isn't valid C# code, so it's very difficult to understand what you're asking. But I think you're trying to understand how the expression this.Hoge is translated into something that can eventually provide you with the name of the method.
If so, then your code example should look something like this:
var methodName = ((Action<bool>)this.Hoge).Method.Name;
And what that does is to implicitly create an instance of a delegate type (in this case, of the type Action<bool>), as if you'd written this:
var methodName = new Action<bool>(this.Hoge).Method.Name;
And of course, once you have a delegate type, that type has a Method property, which returns a MethodInfo object which in turn, of course, has a Name property.
If that is not what you're asking, please improve your question by providing a valid, compilable C# example of what you're asking about, along with a more precisely worded question about that code.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I have got a list of PropertyInfo, now i need to either populate a new object with these propertyinfos and there values or generic a type runtime containing these properties so i can create a new instance of the object based on my runtime created type.
I cannot create a new instance of an object based on an earlier type because i just filtered out my collection based properties (this is due to serialization of an object; long story).
How can i achieve this?
I think TypeBuilder will suit your needs.
On the bottom of the page you will see a clear example.
Also if you need to build methods use Expression Trees instead of ILGenerator.
Here's a few options:
Use TypeBuilder to create a runtime type. Use DefineMethodOverride to implement the get/set methods of the properties and return the interface type (with the runtime implementation).
Use one of the many Mock frameworks. They basically do the plumbing for you; the result is the same.
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
Writing UnitTests with MSTest I want to assert the equality of a return value vs. the one I'm expecting.
Expected type is a custom type that does not implement the IComparable interface nor the IEquatable interface, thats why I want to give Assert.AreEqual a possibility to compare the two objects.
I am aware that this possibility exists in CollectionAssert.AreEqual. This method however requires two ojects that inherit ICollection which my objects do not.
Why does Assert.AreEqual not allow me to specify a custom comparer? Am I missing something?
Not sure if this is the actual reason, but what if your custom IComparer was faulty - your unit test would be meaningless (bearing in mind that the test framework has no way to tell if you wrote unit tests for it let alone if they are "correct")
Could you just create a comparer in your test?
var com = new MyComparer<Foo>();
int expected=0;
int actual = com.Compare(a,b);
if (actual!=0)
{
Assert.Fail("oops");
}
Maybe not ideal, but should work...
I also found this question from a few years ago on msdn, with no answers - but an interesting approach to the workaround by the question poster.
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 9 years ago.
Improve this question
I want to make a a function, and i dont want to write it many times with different types.
Can I use 'where' and "tell it" that I want one of the specific types that I write?
By the way, I need a function to handle integers and another to handle floating numbers.
No, you can't. There are specific constraints you can apply for type parameters, e.g. it must be a non-nullable value type, or it must be a class, or it must implement an interface or whatever... but you can't specify a set of types and say that it must be one of those.
Even if you could do so, I suspect it wouldn't do what you want - because I expect you want to perform arithmetic on these types.
Two options:
If you're using C# 4 you could use dynamic typing. It doesn't give you compile-time safety, but it'll work if you're careful.
You could use Marc Gravell's generic operators in MiscUtil
You can't statically restrict the function to only take floats or integers. To do so, you would have to check the argument types manually inside the function.