How is Complex number assignment from a double enabled? [closed] - c#

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 6 years ago.
Improve this question
The Complex structure in System.Numerics allows assignment like this
Complex c = 3.72;
If I wanted to make my own Complex struct, how would I program this capability? I like it better than using constructors as in
Complex c = new Complex(3.72);

You need to declare an implicit conversion operator.

Thanks Code-Apprentice for the correct answer. I will just post exactly the code I added to my Complex struct
public static implicit operator Complex(double x)
{
return new Complex(x); // implicit conversion
}
This seems to work.
I will clarify why I am doing this. I wrote a large modeling code using Microsoft Complex everywhere. I tried to use the code in a Xamarin forms project but I could not get a reference to System.Numerics. To make progress I had to resurrect an old Complex struct I had made years ago, and get it to match the Microsoft capabilities.

Related

Should we declare a collection class or interface? [closed]

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>.

The trend toward "_field = field;" against "this.field = field" [closed]

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 have seen that the majority of well-known developers (such as those in Lynda, Pluralsight, etc. or even writers) nowadays prefer the following approach over the latter one:
private IField _Field;
publc MyClass (IField Field){
_Field = Field;
}
Over this approach:
private IField Field;
public MyClass(IField Field){
this.Field = Field;
}
I know that there must be a reason behind this, but I can't figure out why.
Since this trend has began by the time C# 6 was released, I guess the reason might be due to something relating to Roslyn compiler but could not find anything over Google.
I'd appreciate if anyone could elaborate what advantages the first approach may possibly have over the second one.
It is just one naming convention - a lot of people coming from C and C++ backgrounds are used to this convention, as it makes it very clear that a field is a field, which is not as easily visible in those languages (given the IDEs that existed when they were invented).
With C# and particularly Visual Studio giving one a lot of help with idenifying members and their scope, this is less needed.

How to make extension for all at once ? [closed]

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 6 years ago.
Improve this question
I have to make extensions for all types arrays of numbers : ints, longs, floats, doubles, etc. that can do some things.
Of course, I could do it for every type of integers, but that would look ugly.
double GetSomeValue (int[] array)
{
// some code
}
double GetSomeValue (double[] array)
{
// some code
}
etc, etc...
Is there any nice way to make in a short manner ?
Short answer is no. Depending on what you are actually trying to do you can use generic method declaration but it will accept broader set of types, cause there is no way to restrict generic method to accept only numeric types in C#.
Also an option would be using T4 Templates like here

How do keywords work in C#? [closed]

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 8 years ago.
Improve this question
I'm not even sure how should I frame this question so that you all get what actually I'm asking for.
I'm wondering how does the keywords work in programming languages, being specific, C#. In the below code:
using System;
namespace TestApplication
{
class Program
{
static void Main(string[] args)
{
string s = "Hello";
Console.WriteLine(a.ToString());
Console.ReadLine();
}
}
}
Here, Console is a predefined class of System namespace which lies in mscorlib.dll. So when the compiler/CLR meets the Console.WriteLine(), it will invoke the static method WriteLine() with appropriate overload.
So the definition of WriteLine method and Console class all are already written and kept in the System namespace of the mscorlib assembly.
But my question is when the complier/CLR meets with the keywords like using,namespace, class,static, what does it do? Where is it written that it must treat the word next to the class keyword as a new type? Is it built in to the compiler/CLR? How does it work then?
C# keywords, which you can see the full list of here, are built into the CSC compller. When the compiler comes across any keyword, it is programmed to know what to expect and what to do.
This is part of the compiler, not the BCL. Check out the language specification which explains exactly what the compiler must do when it encounters any of the keywords.

MSTest - why is there no AreEqual(object, object, IComparer<T>)? [closed]

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.

Categories