Skip to main content

Declare Class With Properties (C#)

  • 2 minutes to read
In This Article

Declares a class with properties initialized to the parameters passed to the constructor. Creates text fields for the constructor parameters, activates Linked Identifiers for the names of declared properties, and drops a marker onto the initial class reference.

The Declare Class With Properties code provider also declares class members called from the initial code.

#Availability

From the context menus or via shortcuts:

  • when the edit cursor or caret is on a reference to a constructor of a non-existent class, provided that the constructor has at least one parameter;
  • when the edit cursor or caret is on a reference to a non-existent class, provided that the current scope contains a reference to its constructor having at least one parameter.

#Example

class TestClass
{
    private void TestMethod()
    {ClassA objVar = new ClassA("ClassName", 25);
        objVar.Name = "ObjName";
        objVar.IntValue = 15;
    }
}

Result:

public class ClassA
{
    public ClassA(string param1, int param2)
    {
        Param1 = param1;
        Param2 = param2;
    }
    public int IntValue { get; set; }
    public string Name { get; set; }
    public string Param1 { get; set; }
    public int Param2 { get; set; }
}
class TestClass
{
    private void TestMethod()
    {
        ClassA objVar = new ClassA("ClassName", 25);
        objVar.Name = "ObjName";
        objVar.IntValue = 15;
    }
}