Skip to content

Commit 0c5c2a3

Browse files
committed
C#: Add implicit to string test.
1 parent 6a406b2 commit 0c5c2a3

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
3+
public class TestImplicitToString
4+
{
5+
public class Container
6+
{
7+
public override string ToString()
8+
{
9+
return "Container";
10+
}
11+
}
12+
13+
public class FormattableContainer : IFormattable
14+
{
15+
public string ToString(string format, IFormatProvider formatProvider)
16+
{
17+
return "Formatted container";
18+
}
19+
20+
21+
public override string ToString()
22+
{
23+
return "Container";
24+
}
25+
}
26+
27+
public void M()
28+
{
29+
var container = new Container();
30+
31+
var y = "Hello" + container; // Implicit ToString call
32+
y = "Hello" + container.ToString();
33+
y = $"Hello {container}"; // Implicit ToString() call
34+
y = $"Hello {container.ToString()}";
35+
y = $"Hello {container:D}"; // Implicit ToString() call.
36+
37+
var z = "Hello" + y; // No implicit ToString call as `y` is already a string.
38+
39+
var formattableContainer = new FormattableContainer();
40+
y = "Hello" + formattableContainer; // Implicit call to ToString().
41+
y = $"Hello {formattableContainer}"; // Implicit call to ToString(string, IFormatProvider). We don't handle this.
42+
y = $"Hello {formattableContainer:D}"; // Implicit call to ToString(string, IFormatProvider). We don't handle this.
43+
}
44+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
| implicitToString.cs:31:27:31:35 | call to method ToString |
2+
| implicitToString.cs:33:22:33:30 | call to method ToString |
3+
| implicitToString.cs:35:22:35:30 | call to method ToString |
4+
| implicitToString.cs:40:23:40:42 | call to method ToString |
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import csharp
2+
3+
from MethodCall c
4+
where c.isImplicit()
5+
select c

0 commit comments

Comments
 (0)