How to make extension for all at once ? [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
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

Related

Creating 100 variables [closed]

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 1 year ago.
The community is reviewing whether to reopen this question as of 1 year ago.
Improve this question
So I created an array with 100 variables using Enumerable.Range. The data type is limited to Int32.
Problem
How can I create the same array with SByte?
Am I right in thinking I would need to use a loop to create and index the variables?
I have looked around online and most results touch on declaring counting variables for the loop but not using a loop to declare variables
Just cast them:
SByte[] array = Enumerable.Range(0, 100).Select(i => (SByte) i).ToArray();
note that SByte is not cls compliant, you might want to use short instead.

how does a symbol table be in case of method overloading 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 3 years ago.
Improve this question
How is a symbol table represented in method overloading?
The compilers that I am familiar with do it by synthesizing a unique name for each overload based on the given name and the types of the arguments.
Say for example you have three overloads for a function, MyFunc:
void MyFunc(int arg1);
void MyFunc(double arg1);
void MyFunc(int arg1, int arg2);
Internally, the compiler might call them
MyFunc$i4
MyFunc$d8
MyFunc$i4$i4
or similar, where the $ delimiter here is some character that isn't allowed in user-defined symbol names, and the suffixes are made up of short-hand code symbols for basic data types (i4 = 4-byte signed integer, d8 = double, and so on).
(The scheme I show here is just something I made up to illustrate the principle. To see how it's actually done, google compiler name mangling).

How is Complex number assignment from a double enabled? [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
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.

Types which can be displayed as string [closed]

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 need to collect all Types in C# which can be converted to string.
My List so far contains:
string
byte
sbyte
char
int
uint
long
ulong
short
ushort
Please help me collect more.
Of the types that you mentioned only string can be truly casted to string. There are no other types, and there could be no other types like that, because System.String class is sealed.
As far as converting, not casting, to string goes, every single type in .NET has a conversion to string available, because ToString method is defined on System.Object, the root class of all objects in .NET. In particular, all primitive types in .NET provide suitable overrides for their ToString method.

Some specific types in generic function [closed]

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.

Categories