Skip to content

Just wonder reason of the casting? #1573

Closed

Description

@akrsnr

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");
}

Activity

  1. mgravell commented on Nov 16, 2020

    @mgravell
    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 👍

  2. added 2 commits that reference this issue on May 13, 2025
    bf31f97
    5cc7438
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions