Visual Studio tips 2
Tags: programming, product, csharp
Published: 2023-02-25 17:28:37

## Visual Studio Generate From Usage feature

This tip focuses on how Visual Studio 2019 or higher provides the ability to create a new class from code a developer has written without actually creating the necessary class.

Example, a developer needs the following class.

```csharp
public class Person
{
    public Person()
    {
    }

    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateOnly BirthDate { get; set; }
    public string Country { get; set; }
    public List<string> PhoneNumbers { get; set; }

}
```
Visual Studio can create the class by writing out a new Person class as shown below.


![Code without Person class](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a15ubo627ltjqinhep29.png)

Since the class and it's properties do not exists they have a foreground color of red.

To create the Person class, hover the mouse over Person as shown below.


![Hover mouse over new Person](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1sjdllduyr0of9dgfjwa.png)

Click on Show potential fixes and the following dialog appears, select Generate new type...


![options context menu](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xfbc7weafz46t9aw63do.png)

In the following dialog, choose access modifier, class or struct, name is disabled.

For location, if there is one project leave as is. For file name, create a new file or place in the current file. Unless there is a good reason for placing the class in the current file, place in a new file.

In this example we are in a standard Console project and want the Person class in a folder named Models which does not exists. To create the Person class under the non-existing Models folder type Models\ in front of Person and press OK. Next, open the new class and update the namespace by appending Models.

In the [video](https://twitter.com/KarenPayneMVP/status/1629513047407751170?s=20), there is a class project without any classes, we want to do what we just did but place the Person class under a folder named Models in the class project.

**Video note**: :video_camera: Would had embedded here but there are too many frames.

Now we know there will be other classes that we want an Id Property so lets create an Interface, type : IBase after Person, hover over Person and select Generate interface IBase, in this case it will be created in the same file.


![create interface](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/51eahwc5ge99wbze1s6x.png)

Add Id property by copy the Id property from the Person class and pasting into the interface.

```csharp
public class Person : IBase
{
    public Person()
    {
    }

    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateOnly BirthDate { get; set; }
    public string Country { get; set; }
    public List<string> PhoneNumbers { get; set; }
    public int Id { get; set; }
}

public interface IBase
{
    public int Id { get; set; }
}
```
Click in the gutter with the mouse cursor on IBase to move to it's own file.

![move interface to a new file](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7kd631hzf1yul6mw7iy9.png)

Create a new folder named Interfaces, in Solution Explorer drag IBase.cs to the Interface folder, done.

## Origins

This functionality comes from TDD (Test Driven development) which is a difficult concept for many developers but given time learning how to write unit test than writing TDD in unit test with proper specifications will extend time to develop an application with the benefits of a better coded application along with using the unit test when sometime breaks, before traversing through code or debugging code run the test methods which very well may point to the problem.

## References

- Microsoft: Walkthrough: [Test-driven development using Test Explorer](https://learn.microsoft.com/en-us/visualstudio/test/quick-start-test-driven-development-with-test-explorer?view=vs-2022)
- Microsoft: Walkthrough: [Test-first development with the Generate From Usage feature](https://learn.microsoft.com/en-us/visualstudio/ide/walkthrough-test-first-support-with-the-generate-from-usage-feature?view=vs-2022&tabs=csharp)

## See also

- Microsoft [Visual Studio Tips & Tricks](https://www.youtube.com/playlist?list=PLReL099Y5nRc-zbaFbf0aNcIamBQujOxP) videos