Skip to content

SA1118: Fails when using new(){...} but not new myObject{...} #3339

Closed

Description

@BlythMeister

Code example:

    public class MyClass
    {
        public class MyObject
        {
            public string MyValue { get; init; }
        }

        public void MyTestFunctionOk()
        {
            MyCallingFunction(0, new MyObject
            {
                MyValue = "Test"
            });
        }

        public void MyTestFunctionNotOk()
        {
            MyCallingFunction(0, new()
            {
                MyValue = "Test"
            });
        }

        public void MyCallingFunction(int index, MyObject myObject)
        {
            Console.WriteLine($"{index}: {myObject}");
        }
    }

these are syntactically the same, except one states the object type in the initialisation, and one does not, yet MyTestFunctionNotOk gives a SA1118 warning

Activity

  1. changed the title [-]SA1118: Fails when using `new{}` but not `new myObject{}`[/-] [+]SA1118: Fails when using `new(){}` but not `new myObject{}`[/+] on Apr 28, 2021
  2. changed the title [-]SA1118: Fails when using `new(){}` but not `new myObject{}`[/-] [+]SA1118: Fails when using `new(){...}` but not `new myObject{...}`[/+] on Apr 28, 2021
  3. added this to the 1.2-beta.next milestone on Apr 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions