CLI ref structs. Is there any benefit to using it.

D

DaTurk

Hi,

I'm coding a layer into my application using CLI, between a unmanaged
and a c# layer. So, I have to marshal some unmanaged c++ structures
to structures usable in c#.

My solution was to marshal the unmanaged C++ structures to CLI ref
structs, these being usable in the c# layer. Why I did it this way is
I want all of the business logic in the CLI layer. Because he c#
layer is purely presentaion.

My question is, what is the point of using CLI ref structs?
 
B

Bruno van Dooren

I'm coding a layer into my application using CLI, between a unmanaged
and a c# layer. So, I have to marshal some unmanaged c++ structures
to structures usable in c#.

My solution was to marshal the unmanaged C++ structures to CLI ref
structs, these being usable in the c# layer. Why I did it this way is
I want all of the business logic in the CLI layer. Because he c#
layer is purely presentaion.

My question is, what is the point of using CLI ref structs?

Hi,

the point of using ref structs is that they translate to classes with public
members on the C# side without the need for marshalling. As a result, you
can pass those structures to the C# side or to .NET methods.
If these structures do not leave the CLI side, there is no point to
marshaling them because C++/CLI is designed to use unmanaged structures and
classes without a problem. In that case, marshaling them to managed
stuctures is redundant.
 
D

DaTurk

Hi,

the point of using ref structs is that they translate to classes with public
members on the C# side without the need for marshalling. As a result, you
can pass those structures to the C# side or to .NET methods.
If these structures do not leave the CLI side, there is no point to
marshaling them because C++/CLI is designed to use unmanaged structures and
classes without a problem. In that case, marshaling them to managed
stuctures is redundant.

--
Kind regards,
Bruno van Dooren MVP - VC++
http://msmvps.com/blogs/vanDooren
(e-mail address removed)

I'm not sure I understand. Why wouldn't I just use a CLI class then.
What's the difference? Is there any real benefit though?
 
B

Ben Voigt

I'm not sure I understand. Why wouldn't I just use a CLI class then.
What's the difference? Is there any real benefit though?

Are you asking about "ref struct vs ref class" or "ref struct vs native
class" or "ref struct vs value struct"?

ref struct and ref class are related in the same way as native struct and
native class -- there is no runtime difference. The only thing affected is
that struct, ref struct, and value struct all start with implicit "public:"
and class, ref class, and value class all start with implicit "private:".
ref class and ref struct are both C++/CLI equivalents to C# struct, all
three generate a garbage collected pass-by-ref CLR type, and all three have
different default member visibility.
 
T

Tamas Demjen

DaTurk said:
I'm not sure I understand. Why wouldn't I just use a CLI class then.
What's the difference? Is there any real benefit though?

C++/CLI -------> C#
====================
ref class -----> class
ref struct ----> class
value class ---> struct
value struct --> struct

The C++/CLI keyword "ref" always indicates a reference type (C# class),
while the "value" keyword always refers to a value type (C# struct).

What confuses you is that in C# class and struct mean completely
different things than in C++. In C++, the only difference between class
and struct is whether members are public or private by default. In C#,
on the other hand, a class is a reference type, and a struct is a value
type, and they have nothing to do with public vs private access.

Tom
 
B

Ben Voigt

Tamas Demjen said:
I think you meant C# class.

Yes indeed.

ref class and ref struct are both C++/CLI equivalents to C# class
value class and value struct are both C++/CLI equivalents to C# struct

Thanks for calling out my typo.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top