-
Notifications
You must be signed in to change notification settings - Fork 220
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
VB -> C#: Ref extension method not allowed in C# #785
Comments
This may work if you only limit the array to one type
|
I think it just needs |
On further reflection, in the example given, even if T is constrained to struct, it's invalid as an extension method in C# because the array isn't a value type, so you'd need to use the ref keyword at the callsite, but can't because the syntax for that doesn't exist. In your specific case, the most idiomatic option is probably to define a method like this: public static T[] Add<T>(this T[] arr, T item)
{
var newArr = arr;
Array.Resize(ref newArr, arr.Length + 1);
arr[arr.Length - 1] = item;
return newArr;
} Then you would call it like: In terms of what the converter can do, I think it's best that it just stops it being an extension method and updates any callers. |
This sample on StackOverflow does not convert
The error is, "CS8337 The first parameter of a 'ref' extension method 'Add' must be a value type or a generic type constrained to struct."
VB.Net input code
Erroneous output
Expected output
//unsure of correct output
Details
CC 8.41
The text was updated successfully, but these errors were encountered: