C# Class subroutines, How to pass the eqivilant of VB's byRef

I have some C subroutines in DLL's that I want to convert to C#. They basically have parms:

Input buffer (bytes)
Input length (int32)
Outbut buffer (bytes)
Output length (int32)

In VB.net, we simply define a subroutine with parameters defined as byRef, for example:

Sub MySub(..., byRef OutBuff() as Byte, byRef outLen as Int32)
        ' Do something
End Sub

This passes the data's pointers to the subroutine (and the length attribute as part of the byte array object). Thus the subroutine does not have any size limit/wasted memory tradeoffs. How do I do this in C#? In my attempts I keep getting "unsafe code", which VB will not allow me to call.

Thank you.
Who is Participating?

Improve company productivity with a Business Account.Sign Up

Umar TopiaConnect With a Mentor .Net Full Stack DeveloperCommented:
Sry abt the typo:

void MySub(ref bytes[] byInput)
Umar Topia.Net Full Stack DeveloperCommented:
function MySub(ref bytes[] byInput)
Fernando SotoRetiredCommented:
Hi MikeBroderick;

When calling the method MySub in umartopia example you also need to have the ref key word.

// Calling MySub method:
MySub ( ... ref myByteArray, ... );

MikeBroderickAuthor Commented:
Thanks. I'm putting it in now.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.