Skip to content

Just wonder reason of the casting? #1573

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

Closed
akrsnr opened this issue Nov 16, 2020 · 1 comment · May be fixed by #2167
Closed

Just wonder reason of the casting? #1573

akrsnr opened this issue Nov 16, 2020 · 1 comment · May be fixed by #2167

Comments

@akrsnr
Copy link

akrsnr commented Nov 16, 2020

var dog = connection.Query<Dog>("select Age = @Age, Id = @Id", new { Age = (int?)null, Id = guid });

Why do you need to cast null to (int?) here?

I can understand the reason for the casting for the below one, but yours.

Main()
{
	T.A(null);
}

public class T {
	public static void A(string x) => Console.WriteLine("str");
	public static void A(List<int> x) => Console.WriteLine("list");
}
@mgravell
Copy link
Member

The new {...} is an anonymous type - the members need to have names and types. Without the cast (which is only a compile-time cast - it evaporates in the IL), the compiler wouldn't know what type to use for the Age member, and compilation would fail. Honestly, take away the cast and just try to compile it 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants