Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 687029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:05:06+00:00 2026-05-14T02:05:06+00:00

The short version of the question – why can’t I do this? I’m restricted

  • 0

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
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T02:05:06+00:00Added an answer on May 14, 2026 at 2:05 am

    “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&gt, 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();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

SHORT VERSION OF QUESTION: So basically my question is: How can I set the
To short version of this question is that I want to accomplish something along
Short version question : Is there navigator.mozIsLocallyAvailable equivalent function that works on all browsers,
Short version of question: see title Long version of question: I've used jquery's show()
Short version of the question: Since I already have TortoiseHg, I right clicked on
Hopefully short version of the question: If I'm on a machine that has a
The short version of my question is how do I change the build order
The short version of the question: I do: x = y . Then I
I have a short version of the question: I start a thread like that:
Short Version of the question: I will put a quad. I know the width

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.