C# 将参数作为C+中的void*传递给方法+;

C# 将参数作为C+中的void*传递给方法+;,c#,C#,我正在寻找一个C夏普的抽象方法示例,它具有通用原型,派生的重写方法可以接收不同的参数结构 来自C的示例++ class IBase { public: virtual void method(void * pParameters) = 0; } class CDerivedA : public IBase { public: struct S_A { char a; short b; } virtual void method(void * pParameter

我正在寻找一个C夏普的抽象方法示例,它具有通用原型,派生的重写方法可以接收不同的参数结构

来自C的示例++

class IBase {
public:
  virtual void method(void * pParameters) = 0;
}

class CDerivedA : public IBase {
public:
  struct S_A {
    char a;
    short b;
  }
  virtual void method(void * pParameters);
}

void CDerivedA::method(void * pParameters) {
  S_A * pSa = (S_A *)pParameters;
  pSa->a
  pSa->b
}

class CDerivedB : public IBase {
public:
  struct S_B {
    long c;
    float d;
    double e;
  }
  virtual void method(void * pParameters);
}

void CDerivedB::method(void * pParameters) {
  S_B * pSb = (S_B *)pParameters;
  pSb->c
  pSb->d
  pSb->e
}
执行速度非常重要,所以我更喜欢通过引用传递

我在C#中找到了两个实现此功能的选项: 一,

二,

witch更好/更快?

因为C是一种托管语言,所以没有所谓的
void*
(即使在C++中这也不是一个好主意)。在C#中有两个选项:传递“对象”或使用泛型。必须将对象强制转换为适当的类型,才能用作除黑盒以外的任何对象。泛型(功能较弱的C#版模板)通常是一种方法

如果需要传递多个参数,可以使用C#
元组
(类似于C
void*
数组)或
params
对象数组(类似于C varargs)

如果没有一个很好的例子来说明为什么这是有用的(如果可能的话),这看起来像是一个糟糕的设计

从评论中我可以看出,在这个话题上有很多困惑,所以让我非常清楚: 在C++和C++中,代码>类>代码> >代码>结构> /代码>非常不同。在C++中,区别是关于字段的构造和公共/私有访问。在C#中,区别在于它们作为参数传递时如何处理

在C#中,当您声明一个类并将“该类”(实际上是该类的一个实例)传递到函数中时,实际上是在将对该类实例的引用传递到函数中。在C++中,没有办法将类实例本身传递到函数中。与C++不同,在C++中,调用的函数可以修改类实例中的数据,而调用方可以看到这种更改,因为在C++中,调用复制构造函数,并且将类实例数据实际复制到堆栈上,但是在C语言中,类是引用类型。p> 以下是一些示例,假设两种语言中的
C
都是
class
S
都是
struct

在C++中:

void func (C c) 
{
     c.value = true; // This change will NOT be seen by the caller because the class was passed by value
}

void func (C* c) 
{
     c->value = true; // This change WILL be seen by the caller because the class was passed by reference (pointer in this case, though the distinction is only syntactic)
     c = new C();     // This new instance will not be seen by the caller and will never be destroyed or freed
}

void func (C& c) 
{
     c.value = true; // This change WILL be seen by the caller because the class was passed by reference
}

void func (C** c) 
{
     (*c)->value = true; // This change will only be seen by the caller if they kept another pointer (reference) to the instance, because the next statement will overwrite the pointer whose pointer was passed into this function
     *c = new C();       // A new instance of C WILL be seen by the caller because the pointer to the class was passed by reference (pointer)
}

void func (S s) 
{
     s.value = true; // This change will NOT be seen by the caller because the struct was passed by value
}

void func (S* s) 
{
     s->value = true; // This change WILL be seen by the caller because the struct was passed by reference (pointer)
     s = new S();     // This new instance will not be seen by the caller and will never be destroyed or freed
}

void func (S& s) 
{
     s.value = true; // This change WILL be seen by the caller because the struct was passed by reference
}

void func (S** s) 
{
     (*s)->value = true; // This change will only be seen by the caller if they kept another pointer (reference) to the instance, because the next statement will overwrite the pointer whose pointer was passed into this function
     *s = new S();       // A new instance of S WILL be seen by the caller because the pointer to the struct was passed by reference (pointer)
}
在C#中:

要按C#中的值传递实际数据(不是引用/指针),必须将数据声明为
struct
。有关更多信息,请参阅。

因为C是一种托管语言,所以没有所谓的
void*
(即使在C++中这也不是一个好主意)。在C#中有两个选项:传递“对象”或使用泛型。必须将对象强制转换为适当的类型,才能用作除黑盒以外的任何对象。泛型(功能较弱的C#版模板)通常是一种方法

如果需要传递多个参数,可以使用C#
元组
(类似于C
void*
数组)或
params
对象数组(类似于C varargs)

如果没有一个很好的例子来说明为什么这是有用的(如果可能的话),这看起来像是一个糟糕的设计

从评论中我可以看出,在这个话题上有很多困惑,所以让我非常清楚: 在C++和C++中,代码>类>代码> >代码>结构> /代码>非常不同。在C++中,区别是关于字段的构造和公共/私有访问。在C#中,区别在于它们作为参数传递时如何处理

在C#中,当您声明一个类并将“该类”(实际上是该类的一个实例)传递到函数中时,实际上是在将对该类实例的引用传递到函数中。在C++中,没有办法将类实例本身传递到函数中。与C++不同,在C++中,调用的函数可以修改类实例中的数据,而调用方可以看到这种更改,因为在C++中,调用复制构造函数,并且将类实例数据实际复制到堆栈上,但是在C语言中,类是引用类型。p> 以下是一些示例,假设两种语言中的
C
都是
class
S
都是
struct

在C++中:

void func (C c) 
{
     c.value = true; // This change will NOT be seen by the caller because the class was passed by value
}

void func (C* c) 
{
     c->value = true; // This change WILL be seen by the caller because the class was passed by reference (pointer in this case, though the distinction is only syntactic)
     c = new C();     // This new instance will not be seen by the caller and will never be destroyed or freed
}

void func (C& c) 
{
     c.value = true; // This change WILL be seen by the caller because the class was passed by reference
}

void func (C** c) 
{
     (*c)->value = true; // This change will only be seen by the caller if they kept another pointer (reference) to the instance, because the next statement will overwrite the pointer whose pointer was passed into this function
     *c = new C();       // A new instance of C WILL be seen by the caller because the pointer to the class was passed by reference (pointer)
}

void func (S s) 
{
     s.value = true; // This change will NOT be seen by the caller because the struct was passed by value
}

void func (S* s) 
{
     s->value = true; // This change WILL be seen by the caller because the struct was passed by reference (pointer)
     s = new S();     // This new instance will not be seen by the caller and will never be destroyed or freed
}

void func (S& s) 
{
     s.value = true; // This change WILL be seen by the caller because the struct was passed by reference
}

void func (S** s) 
{
     (*s)->value = true; // This change will only be seen by the caller if they kept another pointer (reference) to the instance, because the next statement will overwrite the pointer whose pointer was passed into this function
     *s = new S();       // A new instance of S WILL be seen by the caller because the pointer to the struct was passed by reference (pointer)
}
在C#中:

要按C#中的值传递实际数据(不是引用/指针),必须将数据声明为
struct
。有关更多信息,请参阅。

因为C是一种托管语言,所以没有所谓的
void*
(即使在C++中这也不是一个好主意)。在C#中有两个选项:传递“对象”或使用泛型。必须将对象强制转换为适当的类型,才能用作除黑盒以外的任何对象。泛型(功能较弱的C#版模板)通常是一种方法

如果需要传递多个参数,可以使用C#
元组
(类似于C
void*
数组)或
params
对象数组(类似于C varargs)

如果没有一个很好的例子来说明为什么这是有用的(如果可能的话),这看起来像是一个糟糕的设计

从评论中我可以看出,在这个话题上有很多困惑,所以让我非常清楚: 在C++和C++中,代码>类>代码> >代码>结构> /代码>非常不同。在C++中,区别是关于字段的构造和公共/私有访问。在C#中,区别在于它们作为参数传递时如何处理

在C#中,当您声明一个类并将“该类”(实际上是该类的一个实例)传递到函数中时,实际上是在将对该类实例的引用传递到函数中。在C++中,没有办法将类实例本身传递到函数中。与C++不同,在C++中,调用的函数可以修改类实例中的数据,而调用方可以看到这种更改,因为在C++中,调用复制构造函数,并且将类实例数据实际复制到堆栈上,但是在C语言中,类是引用类型。p> 下面是一些示例,假设
C
class
,而
S
S
void func (C c) 
{
     c.value = true; // This change will NOT be seen by the caller because the class was passed by value
}

void func (C* c) 
{
     c->value = true; // This change WILL be seen by the caller because the class was passed by reference (pointer in this case, though the distinction is only syntactic)
     c = new C();     // This new instance will not be seen by the caller and will never be destroyed or freed
}

void func (C& c) 
{
     c.value = true; // This change WILL be seen by the caller because the class was passed by reference
}

void func (C** c) 
{
     (*c)->value = true; // This change will only be seen by the caller if they kept another pointer (reference) to the instance, because the next statement will overwrite the pointer whose pointer was passed into this function
     *c = new C();       // A new instance of C WILL be seen by the caller because the pointer to the class was passed by reference (pointer)
}

void func (S s) 
{
     s.value = true; // This change will NOT be seen by the caller because the struct was passed by value
}

void func (S* s) 
{
     s->value = true; // This change WILL be seen by the caller because the struct was passed by reference (pointer)
     s = new S();     // This new instance will not be seen by the caller and will never be destroyed or freed
}

void func (S& s) 
{
     s.value = true; // This change WILL be seen by the caller because the struct was passed by reference
}

void func (S** s) 
{
     (*s)->value = true; // This change will only be seen by the caller if they kept another pointer (reference) to the instance, because the next statement will overwrite the pointer whose pointer was passed into this function
     *s = new S();       // A new instance of S WILL be seen by the caller because the pointer to the struct was passed by reference (pointer)
}
void func (C c) 
{
     c.value = true; // This change will be seen by the caller because the class was passed by reference
}

void func (ref C c) 
{
     c.value = true;  // This change will only be seen by the caller if they kept another reference to the class instance other than the one they passed by reference to this function, because the next statement will overwrite the reference whose reference was passed into this function
     c = new C();     // A new instance of C WILL be seen by the caller because the reference to the class was passed (by reference)
}

void func (S s) 
{
     s.value = true; // This change will NOT be seen by the caller because the struct was passed by value
}

void func (ref S s) 
{
     s.value = true;  // This change will only be seen by the caller if they kept another reference to the struct instance other than the one they passed by reference to this function, because the next statement will overwrite the reference whose reference was passed into this function.  I think this would require boxing the original struct instance.
     s = new S();     // A new instance of S WILL be seen by the caller because the struct was passed by reference
}