簡體   English   中英

將Pascal'type'轉換為C#

[英]Converting Pascal 'type' to C#

我正在嘗試將Pascal類型轉換為C#。 我在谷歌上看了一下,但我沒有設法找到答案,可能是因為我沒有正確搜索,所以很抱歉,如果這是重復的話。

我有這兩種Pascal類型:

type
  TVector3i = array [0..2] of longint;

  Tcolface = packed record
    A, B, C: word;
    SurfaceA, SurfaceB: word;
  end;

我知道

Tcolface = packed record
  A, B, C: word;
  SurfaceA, SurfaceB: word;
end;

轉換為:

struct Tcolface {
  ushort A, B, C;
  ushort SurfaceA, SurfaceB;
}

TVector3i = array [0..2] of longint;如何TVector3i = array [0..2] of longint; 兌換?

我試圖避免使用/編寫一個類,因為當我轉換其余的Pascal代碼時,它將期望該類型作為數組,並且我試圖避免將其轉換為.x .y和.z。

我確實考慮過做float[] variablename = new float[3]; ,但是一旦我得到List<float[]> variblename就會變得有點復雜。

完整的代碼是:

TVector3i = array [0..2] of Longint;
TVector3f = array [0..2] of Single;
TVector3d = array [0..2] of Double;

TVector4i = array [0..3] of Longint;
TVector4f = array [0..3] of Single;
TVector4d = array [0..3] of Double;

TMatrix3i = array [0..2] of TVector3i;
TMatrix3f = array [0..2] of TVector3f;
TMatrix3d = array [0..2] of TVector3d;

TMatrix4i = array [0..3] of TVector4i;
TMatrix4f = array [0..3] of TVector4f;
TMatrix4d = array [0..3] of TVector4d;

因此,為什么我要避免上課:D

TVector3i = array [0..2] of longint;如何TVector3i = array [0..2] of longint; 兌換?

沒有直接的等價物。 TVector3i是靜態數組的別名。 C#沒有類似的數組別名。 你可以做的最好的事情是聲明一個包含int[]數組的struct ,並提供一個[] 索引器,以便與Pascal代碼更緊密地語法兼容:

struct TVector3i
{
    private int[] arr = new int[3];

    public int this[int i]
    {
        get
        {
            return arr[i];
        }
        set
        {
            arr[i] = value;
        }
    }
}

更新 :根據您的示例,嘗試這樣的事情:

struct TVector3<T>
{
    private T[] arr = new T[3];

    public T this[int i]
    {
        get
        {
            return arr[i];
        }
        set
        {
            arr[i] = value;
        }
    }
}

struct TVector4<T>
{
    private T[] arr = new T[4];

    public T this[int i]
    {
        get
        {
            return arr[i];
        }
        set
        {
            arr[i] = value;
        }
    }
}

using TVector3i = TVector3<int>;
using TVector3f = TVector3<float>;
using TVector3d = TVector3<double>;

using TVector4i = TVector4<int>;
using TVector4f = TVector4<float>;
using TVector4d = TVector4<double>;

using TMatrix3i = TVector3<TVector3i>;
using TMatrix3f = TVector3<TVector3f>;
using TMatrix3d = TVector3<TVector3d>;

using TMatrix4i = TVector4<TVector4i>;
using TMatrix4f = TVector4<TVector4f>;
using TMatrix4d = TVector4<TVector4d>;

將這個作為值類型可能是有充分理由的。 這意味着賦值運算符是值副本而不是引用副本。 結構可能是:

struct Vector3i
{
    int X;
    int Y;
    int Z;
}

您肯定會添加此類型所需的任何方法,以提供對您有用的操作。 例如, []運算符使索引訪問變得方便。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM