I have two classes which have are nearly equal except the data types stored in them. One class contains all double values while other contains all float values.
class DoubleClass
{
double X;
double Y;
double Z;
}
class FloatClass
{
float X;
float Y;
float Z;
}
Now I have a point of DoubleClass which I want to convert to FloatClass.
var doubleObject = new DoubleClass();
var convertedObject = (FloatClass)doubleObject; // TODO: This
One simple way is to make a method which creates a new FloatClass object, fills all values and return it. Is there any other efficient way to do this.
Use a conversion operator:
public static explicit operator FloatClass (DoubleClass c) {
FloatCass fc = new FloatClass();
fc.X = (float) c.X;
fc.Y = (float) c.Y;
fc.Z = (float) c.Z;
return fc;
}
And then just use it:
var convertedObject = (FloatClass) doubleObject;
Edit
I changed the operator to explicit instead of implicit since I was using a FloatClass cast in the example. I prefer to use explicit over implicit so it forces me to confirm what type the object will be converted to (to me it means less distraction errors + readability).
However, you can use implicit conversion and then you would just need to do:
var convertedObject = doubleObject;
Reference
Sounds like you could use generics here:
public class GenericClass<T>
{
T X { get; set; }
T Y { get; set; }
T Z { get; set; }
}
GenericClass<float> floatClass = new GenericClass<float>();
GenericClass<double> doubleClass = new GenericClass<double>();
You can use Conversion Operators to achieve this.
Fr example:
struct FloatClass
{
public FloatClass(DoubleClass dClass) {
//conversion...
}
...
public static explicit operator FloatClass(DoubleClass dClass)
{
FloatClassd = new FloatClass(dClass); // explicit conversion
return d;
}
}
var convertedObject = (FloatClass)doubleObject;
You could add an implicit type conversion operator:
public class DoubleClass
{
public double X;
public double Y;
public double Z;
public static implicit operator FloatClass(DoubleClass d)
{
return new FloatClass { X = (float)d.X, Y = (float)d.Y, Z = (float)d.Z };
}
}
Now this works:
DoubleClass doubleObject = new DoubleClass();
FloatClass convertedObject = doubleObject;
Add a class for thease extention methods :
public static class ExtensionMethods
{
public static T ToObject<T>(this Object fromObject)
{
return JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(fromObject));
}
public static List<T> ToObjectList<T>(this Object fromObject)
{
return JsonConvert.DeserializeObject<List<T>>(JsonConvert.SerializeObject(fromObject));
}
}
Use :
using YourExtentionMethodNamespace;
Class2 obj2 = obj1.ToObject<Class2>();
List<Class2> lst2 = _db.Blogs.ToList().ToObjectList<Class2>();
The simplest way to do this is by using serializer. Use Newtonsoft JSON serializer which works best.
using Newtonsoft.Json;
private void Convert()
{
DoubleClass doubleClass = new DoubleClass {X = 123.123, Y = 321.321, Z = 111.111};
var serializedoubleClass = JsonConvert.SerializeObject(doubleClass);
var floatClass = JsonConvert.DeserializeObject(serializedoubleClass, typeof(FloatClass));
}
Best way for Convert
public static class Extention {
public static string ConvertObjectToJson(this object ob)
{
return JsonConvert.SerializeObject(ob);
}
}
For Usage
var doubleClass = new DoubleClass {
x = 10,
y = 20
};
var floatClass = JsonConvert.DeserializeObject<FloatClass>(doubleClass.ConvertObjectToJson());
Best way is Serialize object and again desalinize it
Related
I would like to create a class type that seems to be a double value but adds some functionality to it.
I know the possibility described here. But this does not allow to assign a value to an existing instance:
public interface IUnit
{
double ToScaled(double v);
double ToSI(double v);
}
public class ScalableNumber
{
public IUnit Unit { get; set; }
double val;
public double SI { get => val; set => val = value; }
public double Scaled { get => Unit?.ToScaled(val)??val; set => val = (Unit == null)? value : Unit.ToSI(value); }
public ScalableNumber(IUnit u)
{
Unit = u;
}
// implicit conversion to double, operates like an implicit get
public static implicit operator double(ScalableNumber x) => x.val;
// how do I implement an implicit set, assignig double to val
// implicit constructor from double is not useful because I lose the unit
public static implicit operator ScalableNumber(double x) => new ScalableNumber(null) { val=x};
// implicit array property is not useful because the implicit value is a single value
public double this[int i] { get => val; set => val = value; }
}
public class Unit:IUnit
{
public double scale = 1;
public double ToScaled(double v) => v/scale;
public double ToSI(double v) => scale * v;
}
public class MyExample
{
IUnit kilometer, hour, angstroemPerWeek;
public ScalableNumber way;
public ScalableNumber time;
public ScalableNumber speed;
public MyExample()
{
kilometer = new Unit() { scale = 1000 };
hour = new Unit() { scale = 3600 };
angstroemPerWeek = new Unit() { scale = 1e-10 / (60 * 60 * 24 * 7) };
way = new ScalableNumber(kilometer);
time = new ScalableNumber(hour);
speed = new ScalableNumber(angstroemPerWeek);
}
public static void ExecuteMyExample()
{
var myExample = new MyExample();
myExample.way.Scaled = 100;
myExample.time.Scaled = 1;
Console.WriteLine(myExample.Speed);
}
public void Calculate()
{
//I would like to have speed in Ångström per week.
//What I get with the present implementation is the speed in meters per second (unscaled)
speed = way / time;
}
public string Speed { get { if (time <= 0) return string.Empty; Calculate(); return speed.Scaled.ToString(); } }
}
Of course, I can code in Calculate():
speed.SI = way / time;
But if I could use the assignment without the .SI, I could re-use a lot of methods that are currently programmed for properties that have the type double.
Is there a way in c# to direct an assignment to a property without adding the property name?
The short answer is that you can't overload the assignment operator.
The long answer is that if you're willing to do something a bit hacky, you can kinda accomplish what you want with generics and an upcoming feature of C# 11, static abstract interface members.
Basically if you wanna keep the unit information, you will have to find somewhere else to store it, so store it in the type itself.
public interface IUnit
{
static abstract double ToScaled(double v);
static abstract double ToSI(double v);
}
// these interfaces exist so you can box the struct or use them as a generic constraint
public interface IScalableNumber
{
double SI { get; set; }
double Scaled { get; set; }
}
public interface IScalableNumber<TSelf>
:
IScalableNumber,
IAdditionOperators<TSelf, double, TSelf>
// more I...Operators interfaces
where TSelf : IScalableNumber<TSelf>
{
static abstract implicit operator TSelf(double x);
}
// changed this to struct because I didn't see a reason for it to remain a class
public struct ScalableNumber<TUnit> : IScalableNumber<ScalableNumber<TUnit>> where TUnit : IUnit
{
private double val;
public double SI { get => val; set => val = value; }
public double Scaled { get => TUnit.ToScaled(val); set => TUnit.ToSI(val); }
public static ScalableNumber<TUnit> operator +(ScalableNumber<TUnit> left, double right) => left.val + right;
public static implicit operator double(ScalableNumber<TUnit> x) => x.val;
public static implicit operator ScalableNumber<TUnit>(double x) => new() { val = x };
public ScalableNumber<TNewUnit> WithDifferentUnit<TNewUnit>() where TNewUnit : IUnit => val;
}
public abstract class KilometerUnit : IUnit
{
private const double scale = 1000;
public static double ToScaled(double v) => v / scale;
public static double ToSI(double v) => scale * v;
}
public abstract class HourUnit : IUnit
{
private const double scale = 3600;
public static double ToScaled(double v) => v / scale;
public static double ToSI(double v) => scale * v;
}
public abstract class AngstroemPerWeekUnit : IUnit
{
private const double scale = 1e-10 / (60 * 60 * 24 * 7);
public static double ToScaled(double v) => v / scale;
public static double ToSI(double v) => scale * v;
}
public class MyExample
{
public ScalableNumber<KilometerUnit> way;
public ScalableNumber<HourUnit> time;
public ScalableNumber<AngstroemPerWeekUnit> speed;
public MyExample()
{
way = new();
time = new();
speed = new();
}
public static void ExecuteMyExample()
{
var myExample = new MyExample();
myExample.way.Scaled = 100;
myExample.time.Scaled = 1;
Console.WriteLine(myExample.Speed);
}
public void Calculate()
{
speed = way / time;
//now after the assignment, speed is still in 'AngstroemPerWeek'
// in order to change between units do this
way = time.WithDifferentUnit<KilometerUnit>();
// implicit assignment won't work with the non generic interface:
IScalableNumber speedBoxed = speed;
speedBoxed = 10.0;
// all of these should work flawlessly though :) ... and allocate no heap memory
IncrementScalable(ref way);
IncrementScalable(ref time);
IncrementScalable(ref speed);
}
public void IncrementScalable<TScalable>(ref TScalable someNumber) where TScalable : IScalableNumber<TScalable>
{
someNumber += 1;
}
public string Speed { get { if (time <= 0) return string.Empty; Calculate(); return speed.Scaled.ToString(); } }
}
I have a class called GenericItem (first time using generics), suppose i wanted to multiply two items if they were of the type integer, as you can see I am trying it in the method returnCounterMultiply, but it does not allow me to multiply them although i am trying to convert them and also checking if they are of type integer.
namespace Components
{
public class GenericItem<T>
{
private T data;
private T counter;
public T Data
{
get { return data; }
set { data = value; }
}
public GenericItem(){}
public GenericItem(T _data)
{
data = _data;
}
public T returnCounterMultiply(T value)
{
int c = 0;
int d = 0;
if (counter.GetType() == typeof(int) && value.GetType() == typeof(int))
{
//cant multiply two of type T, why if i am converting to int?.
return (T)Convert.ChangeType(counter, typeof(Int32)) * (T)Convert.ChangeType(value, typeof(Int32));
}
return value;
}
}
}
I would appreciate some explanation on this as this is the first time I am working on it (this is just a sample class for understanding this GENERICS INTRO and this GENERICS CLASSES, but still having trouble understanding it.
I don't see what your trying to achieve, but if you have to do it I think you have to use an interface:
public interface IMultiplyable<T>
{
T Multiply(T x);
}
public class Int : IMultiplyable<Int>
{
private int _data { get; set; }
public Int(int data)
{
_data = data;
}
public Int Multiply(Int x)
{
return new Int(_data * x._data);
}
public override string ToString()
{
return _data.ToString();
}
}
public class GenericItem<T> where T : IMultiplyable<T>
{
private T data;
private T counter;
public T Data
{
get { return data; }
set { data = value; }
}
public GenericItem() { }
public GenericItem(T _data)
{
data = _data;
}
public T returnCounterMultiply(T value)
{
return Data.Multiply(value);
}
public override string ToString()
{
return Data.ToString();
}
}
Usage:
var a = new GenericItem<Int>(new Int(4));
MessageBox.Show(a.returnCounterMultiply(new Int(5)).ToString()); //20
In my opinion, using generics in this case is an overkill.
It would be nice that generic constraints support something like:
// T parameter is a type which overloads "+" operator...
where T : +
In your concrete case, I would argue you're going in the wrong way. Why don't you just create a class to implement such math operations where properties are typed as int?
Generics work better when T parameter (or any other parameter, of course...) can be constrained to receive types which have:
A public parameterless constructor.
Inherits or implements a class/interface
You need to constraint that T must be a class and not a struct...
When you go into a problem when using generics requires a type conversion, I believe you defeated the point of generics!
You can do something like this:
public class GenericItem<T>
{
private T data;
public T Data
{
get { return data; }
set { data = value; }
}
public GenericItem(){}
public GenericItem(T _data)
{
data = _data;
}
private Dictionary<Type, Delegate> operations =
new Dictionary<Type, Delegate>()
{
{ typeof(int), (Func<int, int, int>)((x, y) => x * y) },
{ typeof(string), (Func<string, string, string>)((x, y) => x + " " + y) },
};
public T returnCounterMultiply(T value)
{
if (operations.ContainsKey(typeof(T)))
{
var operation = (Func<T, T, T>)(operations[typeof(T)]);
return operation(data, value);
}
return value;
}
}
You just need to define, in the dictionary, one operation per valid types you're going to want to use and it just works without any converting of types (except to cast to the Func).
I had these test results:
var gii = new GenericItem<int>(42);
var xi = gii.returnCounterMultiply(2);
// xi == 84
var gis = new GenericItem<string>("Foo");
var xs = gis.returnCounterMultiply("Bar");
// xs == "Foo Bar"
Your problem has nothing to do with generics but with basic C# casting priority:
//cant multiply two of type T, why if i am converting to int?.
return
(T)Convert.ChangeType(counter, typeof(Int32))
*
(T)Convert.ChangeType(value,typeof(Int32));
You do not multiply int but T - and T being a generic type you can only use methods that are ddefined in your generics contraint, which you have none, so no multiply on it.
If you want to multiply int, then do so:
(T) (
((Int32)Convert.ChangeType(counter, typeof(Int32)))
*
((Int32)Convert.ChangeType(value,typeof(Int32)))
);
See the difference?
Basically in your code you deal with T in the multiplication, here I deal with Int32. And factually if T is a Int32 (as you tested before in the IF statement) you can just skip the convert and cast:
(T) (
((Int32)counter)
*
((Int32)value)
);
Now, generics are a bad example for maths as you can not use operations on generics - sadly. This is an abuse of the concept, but I take it was meant as a learning exercise and thus focused on that part on my answer.
I too tried this once and had to find out that there is no pretty way to do it with generics. You cannot do it as generic as in C++.
As an alternative, you may wrap your data types and use a common interface:
interface IMathOps
{
object Value { get; }
void Add(IMathOps other);
// other methods for substraction etc.
}
class IntWrapper : IMathOps
{
public int value;
public void Add(IMathOps other)
{
if(other is IntWrapper)
{
this.value += (int)other.Value;
}
}
public object Value { get { return this.value; } }
}
// class FloatWrapper : IMathOps ...
I think you should use where (generic type constraint). So it will give error at compile time if T is not int.
public T returnCounterMultiply(T value) where T : int
{
int c = 0;
int d = 0;
return c*d;
}
I have an abstract class, Vector, which I would like to overload the operators +,-,*, etc.
I want any derived classes to be able to use these, and get an object back with the same type as the calling object.
I tried with generics, (as follows, in brief), but I couldn't find a legal way to do it:
public static T operator +<T>( T V1, T V2) where T : Vector
{
//some calculation
return new T(args);
}
I then tried to do it just using the base class:
public static Vector operator+(Vector V1, Vector V2)
{
if (V1.Dimension != V2.Dimension)
throw new VectorTypeException("Vector Dimensions Must Be Equal");
double[] ArgList = new double[V1.Dimension];
for (int i = 0; i < V1.Dimension; i++) { ArgList[i] = V1[i] + V2[i]; }
return (Vector)Activator.CreateInstance(V1.GetType(), new object[] { ArgList});
}
If this method is passed in two child objects, it should perform the operation on them, and return a new object of the same heritage.
The problem I ran into with this is that I cannot enforce that all such child classes must have a constructor with the appropriate signature, and I can't call the base constructor to make the object.
What are ways to either (a) Make either of these work, or (b) do this elegantly in another way?
You could declare instance-level abstract methods which your subclass can override:
public abstract class Vector
{
protected abstract Vector Add(Vector otherVector);
public static Vector operator +(Vector v1, Vector v2)
{
return v1.Add(v2);
}
}
public class SubVector : Vector
{
protected override Vector Add(Vector otherVector)
{
//do some SubVector addition
}
}
Might run into some issues especially with multiple subclasses (Will SubVector have to know how to add with SomeOtherSubVectorClass? What if you add ThirdVectorType class?) and perhaps handling null cases. Also, making sure that SubVector.Add behaves the same as SomeOtherSubVectorClass.Add when it comes to commutative operations.
EDIT: based on your other comments, you could so something like:
public class Vector2D : Vector
{
public double X { get; set; }
public double Y { get; set; }
protected override Vector Add(Vector otherVector)
{
Vector2D otherVector2D = otherVector as Vector2D;
if (otherVector2D != null)
return new Vector2D() { X = this.X + otherVector2D.X, Y = this.Y + otherVector2D.Y };
Vector3D otherVector3D = otherVector as Vector3D;
if (otherVector3D != null)
return new Vector3D() { X = this.X + otherVector3D.X, Y = this.Y + otherVector3D.Y, Z = otherVector3D.Z };
//handle other cases
}
}
public class Vector3D : Vector
{
public double X { get; set; }
public double Y { get; set; }
public double Z { get; set; }
protected override Vector Add(Vector otherVector)
{
Vector2D otherVector2D = otherVector as Vector2D;
if (otherVector2D != null)
return new Vector3D() { X = this.X + otherVector2D.X, Y = this.Y + otherVector2D.Y, Z = this.Z };
Vector3D otherVector3D = otherVector as Vector3D;
if (otherVector3D != null)
return new Vector3D() { X = this.X + otherVector3D.X, Y = this.Y + otherVector3D.Y, Z = this.Z + otherVector3D.Z };
//handle other cases
}
}
EDITx2:
Given your latest comment, perhaps your should just maintain an internal array/matrix and just do generic matrix math. Your subclasses can expose X/Y/Z property wrappers against the array indicies:
public class Vector
{
protected double[] Values;
public int Length { get { return Values.Length; } }
public static Vector operator +(Vector v1, Vector v2)
{
if (v1.Length != v2.Length)
{
throw new VectorTypeException("Vector Dimensions Must Be Equal");
}
else
{
//perform generic matrix addition/operation
double[] newValues = new double[v1.Length];
for (int i = 0; i < v1.Length; i++)
{
newValues[i] = v1.Values[i] + v2.Values[i];
}
//or use some factory/service to give you a Vector2D, Vector3D, or VectorND
return new Vector() { Values = newValues };
}
}
}
public class Vector2D : Vector
{
public double X
{
get { return Values[0]; }
set { Values[0] = value; }
}
public double Y
{
get { return Values[1]; }
set { Values[1] = value; }
}
}
public class Vector3D : Vector
{
public double X
{
get { return Values[0]; }
set { Values[0] = value; }
}
public double Y
{
get { return Values[1]; }
set { Values[1] = value; }
}
public double Z
{
get { return Values[2]; }
set { Values[2] = value; }
}
}
EDITx3: Based on your latest comment, I guess you could implement operator overloads on each subclass, do the shared logic in a static method (say in the base Vector class), and somewhere do a switch/case check to provide a specific subclass:
private static Vector Add(Vector v1, Vector v2)
{
if (v1.Length != v2.Length)
{
throw new VectorTypeException("Vector Dimensions Must Be Equal");
}
else
{
//perform generic matrix addition/operation
double[] newValues = new double[v1.Length];
for (int i = 0; i < v1.Length; i++)
{
newValues[i] = v1.Values[i] + v2.Values[i];
}
//or use some factory/service to give you a Vector2D, Vector3D, or VectorND
switch (newValues.Length)
{
case 1 :
return new Vector1D() { Values = newValues };
case 2 :
return new Vector2D() { Values = newValues };
case 3 :
return new Vector3D() { Values = newValues };
case 4 :
return new Vector4D() { Values = newValues };
//... and so on
default :
throw new DimensionOutOfRangeException("Do not support vectors greater than 10 dimensions");
//or you could just return the generic Vector which doesn't expose X,Y,Z values?
}
}
}
Then your subclasses would have:
public class Vector2D
{
public static Vector2D operator +(Vector2D v1, Vector2D v2)
{
return (Vector2D)Add(v1, v2);
}
}
public class Vector3D
{
public static Vector3D operator +(Vector3D v1, Vector3D v2)
{
return (Vector3D)Add(v1, v2);
}
}
Some duplication, but I don't see a way around it off the top of my head to allow the compiler to do this:
Vector3 v1 = new Vector3(2, 2, 2);
Vector3 v2 = new Vector3(1, 1, 1);
var v3 = v1 + v2; //Vector3(3, 3, 3);
Console.WriteLine(v3.X + ", " + v3.Y + ", " + v3.Z);
or for other dimensions:
Vector2 v1 = new Vector2(2, 2);
Vector2 v2 = new Vector2(1, 1);
var v3 = v1 + v2; //Vector2(3, 3, 3);
Console.WriteLine(v3.X + ", " + v3.Y); // no "Z" property to output!
What about having an abstract method called Add() that operator+ just acts as a wrapper for? ie, "return v1.Add(v2)". This would also enable you to define interfaces which non-Vector classes can constrain their code to, enabling to perform math-like operations (since generic code can't see/touch operators like +, -, etc for any type).
The only constructor you can code with in a generic method is the default (ie, parameter-less) constructor, which you have to specify in the generic constraints for the method/type.
Five years later I had the exact same problem, only I was calling them Ntuples, not vectors. Here is what I did:
using System;
using System.Collections.Generic;
public class Ntuple{
/*parent class
has an array of coordinates
coordinate-wise addition method
greater or less than in dictionary order
*/
public List<double> Coords = new List<double>();
public int Dimension;
public Ntuple(List<double> Input){
Coords=Input;
Dimension=Input.Count;
}//instance constructor
public Ntuple(){
}//empty constructor, because something with the + overload?
public static Ntuple operator +(Ntuple t1, Ntuple t2)
{
//if dimensions don't match, throw error
List<double> temp = new List<double>();
for (int i=0; i<t1.Dimension; i++){
temp.Add(t1.Coords[i]+t2.Coords[i]);
}
Ntuple sum = new Ntuple(temp);
return sum;
}//operator overload +
public static bool operator >(Ntuple one, Ntuple other){
//dictionary order
for (int i=0; i<one.Dimension; i++){
if (one.Coords[i]>other.Coords[i]) {return true;}
}
return false;
}
public static bool operator <(Ntuple one, Ntuple other){
//dictionary order
for (int i=0; i<one.Dimension; i++){
if (one.Coords[i]<other.Coords[i]) {return true;}
}
return false;
}
}//ntuple parent class
public class OrderedPair: Ntuple{
/*
has additional method PolarCoords, &c
*/
public OrderedPair(List<double> Coords) : base(Coords){}
//instance constructor
public OrderedPair(Ntuple toCopy){
this.Coords=toCopy.Coords;
this.Dimension=toCopy.Dimension;
}
}//orderedpair
public class TestProgram{
public static void Main(){
List<double> oneCoords=new List<double>(){1,2};
List<double> otherCoords= new List<double>(){2,3};
OrderedPair one = new OrderedPair(oneCoords);
OrderedPair another = new OrderedPair(otherCoords);
OrderedPair sum1 = new OrderedPair(one + another);
Console.WriteLine(one.Coords[0].ToString()+one.Coords[1].ToString());
Console.WriteLine(sum1.Coords[0].ToString()+sum1.Coords[1].ToString());
bool test = one > another;
Console.WriteLine(test);
bool test2 = one < another;
Console.WriteLine(test2);
}
}
}//namespace ntuples
I am working on a generic utility method that takes a generic argument and returns a generic type--I hope that makes sense!--but I want the return type to be a different type from the argument.
Here's what I'm thinking this should look like if I mock it up in pseudo code:
public static IEnumerable<R> DoSomethingAwesome<T>(T thing)
{
var results = new List<R>();
for (int xx = 0; xx < 5; xx++)
{
results.Add(thing.ToRType(xx));
}
return results;
}
With generics not being able to infer the return type how would I go about doing something like this? So far, my Google-Fu has failed me.
// You need this to constrain T in your method and call ToRType()
public interface IConvertableToTReturn
{
object ToRType(int someInt);
}
public static IEnumerable<TReturn> DoSomethingAwesome<T, TReturn>(T thing)
where T : IConvertableToTReturn
{
Enumerable.Range(0, 5).Select(xx => thing.ToRType(xx));
}
You can pass the return class as an output parameter:
public static void DoSomethingAwesome<T,R>(T thing, out IEnumerable<R> output)
This can then be inferred.
static IEnumerable<R> Function<T,R> (T h)
{
for (int xx = 0; xx < 5; xx++)
{
yield return h.ToRType(xx);
}
yield return break;
}
IEnumerable<class2> res = Function<class1, class2>(class1Object);
You need to explicitly specify the return generic type as a type parameter to the method.
Something like:
public static IEnumerable<R> DoSomething<T,R>(IEnumerable<T> things, Func<T,R> map)
{
foreach (var t in things) { yield return map(t); }
}
This is essentially what the Linq IEnumerable extension method "Select" does..
Generics can be awesome and a pretty awesome pain. As other have stated you can use a variety of ways to have multiple in put parameters the real trick is in doing something usefully with the passed in types.
in Your example
public static IEnumerable<Ret> Fn<Ret,Parm>(IList<Parm> P)
{
var Results = new List<Ret>();
foreach(Parm p in P)
{
Results.Add(p.ToType());
}
return Results;
}
Will not complie since the complier doesn't know what to do with P.ToType()
So you say well I can just add the function needed to my param type But that doesn't work either since the complier again doesn't know what the concrete version or Ret will be and your return list is of type Ret not of type returnType
public class RetunType
{
public int a;
}
public class Input
{
public int x;
public RetunType TotoAReturnType()
{
return new RetunType() { a = this.x };
}
}
public static IEnumerable<Ret> Fn<Ret, Parm>(IList<Parm> P) where Parm : Input where Ret:RetunType
{
var Results = new List<Ret>();
foreach (Parm p in P)
{
Results.Add(p.TotoAReturnType());
}
return Results;
}
To solve this issue you can add a generic interface so that your function can work if any type supports the generic interface
Like this
public interface ToType<R>
{
R ToType();
}
public class B
{
public int x;
}
public class A : ToType<B>
{
string x = "5";
public B ToType()
{
B aB = new B();
aB.x = int.Parse(x);
return aB;
}
}
public static IEnumerable<Ret> Fn<Ret,Parm>(IList<Parm> P) where Parm : ToType<Ret>
{
var Results = new List<Ret>();
foreach(Parm p in P)
{
Results.Add(p.ToType());
}
return Results;
}
static void Main(string[] args)
{
List<A> inLst = new List<A>() { new A()};
var lst = Fn<B, A>(inLst);
}
Generics are awesome but I would strongly suggest looking to using interfaces to support you actions in those functions.
The short version of the question - why can't I do this? I'm restricted to .NET 3.5.
T[] genericArray;
// Obviously T should be float!
genericArray = new T[3]{ 1.0f, 2.0f, 0.0f };
// Can't do this either, why the hell not
genericArray = new float[3]{ 1.0f, 2.0f, 0.0f };
Longer version -
I'm working with the Unity engine here, although that's not important. What is - I'm trying to throw conversion between its fixed Vector2 (2 floats) and Vector3 (3 floats) and my generic Vector<> class. I can't cast types directly to a generic array.
using UnityEngine;
public struct Vector<T>
{
private readonly T[] _axes;
#region Constructors
public Vector(int axisCount)
{
this._axes = new T[axisCount];
}
public Vector(T x, T y)
{
this._axes = new T[2] { x, y };
}
public Vector(T x, T y, T z)
{
this._axes = new T[3]{x, y, z};
}
public Vector(Vector2 vector2)
{
// This doesn't work
this._axes = new T[2] { vector2.x, vector2.y };
}
public Vector(Vector3 vector3)
{
// Nor does this
this._axes = new T[3] { vector3.x, vector3.y, vector3.z };
}
#endregion
#region Properties
public T this[int i]
{
get { return _axes[i]; }
set { _axes[i] = value; }
}
public T X
{
get { return _axes[0];}
set { _axes[0] = value; }
}
public T Y
{
get { return _axes[1]; }
set { _axes[1] = value; }
}
public T Z
{
get
{
return this._axes.Length < 2 ? default(T) : _axes[2];
}
set
{
if (this._axes.Length < 2)
return;
_axes[2] = value;
}
}
#endregion
#region Type Converters
public static explicit operator Vector<T>(Vector2 vector2)
{
Vector<T> vector = new Vector<T>(vector2);
return vector;
}
public static explicit operator Vector<T>(Vector3 vector3)
{
Vector<T> vector = new Vector<T>(vector3);
return vector;
}
#endregion
}
"Generic" means "works with any type".
Your example code is not generic, because it only works if and only if T is float.
While you can't convert a Vector2D to a Vector<T>, you can, of course, convert a Vector2D to a Vector<float>. Add a Convert method to Vector2D or provide a set of extension methods like this:
public static class VectorExtensions
{
public static Vector<float> ToGenericVector(this Vector2D vector)
{
return new Vector<float>(vector.X, vector.Y);
}
public static Vector2D ToVector2D(this Vector<float> vector)
{
return new Vector2D(vector.X, vector.Y);
}
}
Usage:
Vector<float> v = new Vector<float>(3, 5);
Vector2D v2 = v.ToVector2D();
if T is defined as float, via Vector<T> as Vector<float> then this will work (on a restricted T), but if you just want a local conversion:
var genericArray = new float[3]{ 1.0f, 2.0f, 0.0f };
Of course, this restricts T to being a float anyway (the compiler can't convert just anything to T and knows this), it looks like you should replace T with float in the whole class if that's the case, are you dealing with non-float vectors?
In that case you need something like:
var genericArray = new T[3]{ X, Y, Z };
You cannot imply the type of a generic parameter from within a method.
And, as I stated before, your posted code does not represent a valid usage of Generic type parameters.
The generic parameter is to be defined in the class or method signature.
public class Class1<T>
{
public T[] Method(params T[] args)
{
return args;
}
}
public class Demo
{
public Demo()
{
var c1 = new Class1<float>();
float[] result = c1.Method(1.1f, 2.2f);
}
}
You said:
// This doesn't work
this._axes = new T[2] { vector2.x, vector2.y };
The following works (since everything can be converted to object, and the subsequent conversion from object to T is permitted but may fail at runtime if the types aren’t compatible, or in this case if unboxing cannot be performed):
this._axes = new T[2] { (T)(object)vector2.x, (T)(object)vector2.y };
That said, it makes absolutely no sense to make the class generic.