When an object is equal to another object in test methods? [duplicate] - c#

This question already has answers here:
C# - Asserting two objects are equal in unit tests
(8 answers)
Comparing Two objects using Assert.AreEqual()
(5 answers)
Closed 4 years ago.
I want to know, when an object is equal or not equal to another object with the method Assert.AreEqual.
I have to objects, which are tested false.
But when I test every single property with AreEqual the tests are successfull.
That would mean equality does not only depend on properties. Could someone explain that?
(It's a theoretical question, I cannot provide any data)

Related

For methods that need to return multiple values, is it better practice to use tuples, out parameters? [duplicate]

This question already has answers here:
Return multiple values to a method caller
(28 answers)
Is there a way I can return more than one integer from a method? [duplicate]
(5 answers)
Closed 2 years ago.
When my method needs to return more than one value, I can use out parameters or tuples.
Is this a personal preference or do we have a recommended practice documentation how to choose between the two options?
Prefer not to use a complex class type for every o such scenario, because my solution will be bloated with so many classes.
I understand that TryParse is an example exists in .net framework, however it was introduced before tuples.
I think that if you have many results that you want to return, ValueTuple ( for .NET Framesowrk 4.7 and above ) may be better for you because is a struct and immutable, especially if you have many results to return, or you use some kind of API like LINQ and you dont want to create objects of all the result.
You can read also :
Returning two values, Tuple vs 'out' vs 'struct'
What's the difference between System.ValueTuple and System.Tuple?

How to check if a interface reference is a specific class [duplicate]

This question already has answers here:
Type Checking: typeof, GetType, or is?
(15 answers)
Checking if the object is of same type
(4 answers)
How can I check if an object is of a certain type at runtime in C#?
(9 answers)
Closed 3 years ago.
I was wondering if there is a way to find if my interface reference is a specific class.
For example i have DeviceInterface reference, and Playstation, PC and Mac all implement it. Is there a way to see if DeviceInterface is a PC?
I have thought about using a enum to define the type and using that, but is there a way of avoiding this and using a type check or something along those lines?
Thanks in advance.
Let's say that you have
DeviceInterface PcDevice = new PC();
In that case you can just do:
if (PcDevice is PC) { console.WriteLine("I'm a PC"); }
read more here to understand the is and as operators better

Is there a way to get all the possible return values of a method in C#? [duplicate]

This question already has an answer here:
Is using static bytecode analysis to determine all the possible paths through a given method a variant of trying to solve the Halting Problem?
(1 answer)
Closed 4 years ago.
Suppose there's a method which returns enum. But it returns only a subset of all the values of the enumeration. Can I find out programmatically which values are obtainable?
Example. I have an enum, which describes color with 100 values. Method GetCurrentTrafficLightsState can return only 3 colors of 100. I want to pass the method GetCurrentTrafficLightsState into some other method and get 3 colors as response.
No there isn't. You cannot even determine if it will return at all, see https://en.wikipedia.org/wiki/Halting_problem

Difference between ContentPropertyAttribute and ContentProperty [duplicate]

This question already has answers here:
What's the difference between [Something] and [SomethingAttribute] [duplicate]
(3 answers)
Closed 7 years ago.
I hope this wasn't asked already. But i found nothing. If something exists, thanks for the note.
The title says it all i think.
I've seen these two variants. But in my opinion it does the same. And why can i use both. Thanks for education.
// variant 1
[ContentProperty("Text")]
// variant 2
[ContentPropertyAttribute("Text")]
You can omit the word "Attribute" when writing attributes over something. The actual class is called ContentPropertyAttribute. Both of your lines do exactly the same and use the exact same attribute class.

Why does C# make a distinction between ref and out? [duplicate]

This question already has answers here:
What's the difference between the 'ref' and 'out' keywords?
(28 answers)
Why ref and out in C#?
(7 answers)
Closed 9 years ago.
As per this post, the reason there is a distinction between ref and out is because it is costly to copy the value of the variable when using ref.
Why is there a need to marshall in the first place? Doesn't C# just pass the pointer under the hood? In that case, there would be no need to copy values.
Because the semantics of the two are completely different.
An out parameter is used to indicate that it will be used to return (output) a value, nothing more.
A ref parameter on the other hand indicates that an existing object (variable) should be passed to the method by reference. In the context of C#, an object passed by reference (not to be confused by reference types) is often a hint that the method will (and should) modify that object. It shouldn't be used "just because." It is generally used only for value types since it is the only way to get reference semantics for them.

Categories