Skip to content

Commit 781de29

Browse files
Nothing is the default, don't bother specifying since we can't get it right for structs - fixes #1056
1 parent 8d57a1c commit 781de29

2 files changed

Lines changed: 20 additions & 6 deletions

File tree

CodeConverter/CSharp/ExpressionNodeVisitor.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1408,10 +1408,12 @@ public override async Task<CSharpSyntaxNode> VisitParameter(VBSyntax.ParameterSy
14081408
var arg = CommonConversions.CreateAttributeArgumentList(SyntaxFactory.AttributeArgument(defaultExpression));
14091409
_extraUsingDirectives.Add("System.Runtime.InteropServices");
14101410
_extraUsingDirectives.Add("System.Runtime.CompilerServices");
1411-
var optionalAttributes = new[] {
1411+
var optionalAttributes = new List<AttributeSyntax> {
14121412
SyntaxFactory.Attribute(SyntaxFactory.IdentifierName("Optional")),
1413-
SyntaxFactory.Attribute(SyntaxFactory.IdentifierName("DefaultParameterValue"), arg)
14141413
};
1414+
if (!node.Default.Value.IsKind(VBasic.SyntaxKind.NothingLiteralExpression)) {
1415+
optionalAttributes.Add(SyntaxFactory.Attribute(SyntaxFactory.IdentifierName("DefaultParameterValue"), arg));
1416+
}
14151417
attributes.Insert(0,
14161418
SyntaxFactory.AttributeList(SyntaxFactory.SeparatedList(optionalAttributes)));
14171419
} else {

Tests/CSharp/ExpressionTests/ByRefTests.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -347,9 +347,9 @@ public static bool CallingFunc()
347347
public async Task RefAfterOptionalArgumentAsync()
348348
{
349349
await TestConversionVisualBasicToCSharpAsync(@"
350-
Sub S(Optional a As Integer = 0, Optional ByRef b As Integer = 0)
351-
S()
352-
End Sub
350+
Sub S(Optional a As Integer = 0, Optional ByRef b As Integer = 0)
351+
S()
352+
End Sub
353353
", @"
354354
public void S([Optional, DefaultParameterValue(0)] int a, [Optional, DefaultParameterValue(0)] ref int b)
355355
{
@@ -358,6 +358,18 @@ public void S([Optional, DefaultParameterValue(0)] int a, [Optional, DefaultPara
358358
}");
359359
}
360360

361+
[Fact]
362+
public async Task DateRefAfterOptionalArgumentAsync()
363+
{
364+
await TestConversionVisualBasicToCSharpAsync(@"
365+
Sub S(Optional ByRef dt As Date = Nothing)
366+
End Sub
367+
", @"
368+
public void S([Optional] ref DateTime dt)
369+
{
370+
}");
371+
}
372+
361373
[Fact]
362374
public async Task OutOptionalArgumentAsync()
363375
{
@@ -378,7 +390,7 @@ End Sub
378390
379391
public partial class OptionalOutIssue882
380392
{
381-
private void TestSub(out int a, [Optional, DefaultParameterValue(default(int))] out int b)
393+
private void TestSub(out int a, [Optional] out int b)
382394
{
383395
a = 42;
384396
b = 23;

0 commit comments

Comments
 (0)