The requested page is not available for the requested platform. You are viewing the content for Default platform.
All docs
V19.1
19.1
18.2
The page you are viewing does not exist in version 18.2. This link will take you to the root page.
18.1
The page you are viewing does not exist in version 18.1. This link will take you to the root page.
17.2
The page you are viewing does not exist in version 17.2. This link will take you to the root page.

CRR0047 - The type can be moved to a separate file

  • 3 min to read

This analyzer identifies types whose names differ from the file name. These types can be moved to a separate file (with the same name as the type) if a file contains two or more types. This improves code readability.

//PluginManager.cs

using System;
using System.Collections.Generic;

namespace MySolution
{
    public class PluginManager
    {
        public List<PluginInfo> Plugins { get; private set; }
        public void AddPlugin(PluginInfo plugin)
        {
            if (Plugins == null)
                Plugins = new List<PluginInfo>();
            Plugins.Add(plugin);
        }
    }
    public class PluginInfo
    {
        public PluginInfo(string name, string path, bool enabled)
        {
            Name = name;
            Path = path;
            Enabled = enabled;
        }
        public string Name { get; set; }
        public string Path { get; set; }
        public bool Enabled { get; set; }
    }
}

To fix the issue, move the type declaration to a separate file:

//PluginManager.cs

using System;
using System.Collections.Generic;

namespace MySolution
{
    public class PluginManager
    {
        public List<PluginInfo> Plugins { get; private set; }
        public void AddPlugin(PluginInfo plugin)
        {
            if (Plugins == null)
                Plugins = new List<PluginInfo>();
            Plugins.Add(plugin);
        }
    }
}
//PluginInfo.cs

using System;
using System.Collections.Generic;

namespace MySolution
{
    public class PluginInfo
    {
        public PluginInfo(string name, string path, bool enabled)
        {
            Name = name;
            Path = path;
            Enabled = enabled;
        }
        public string Name { get; set; }
        public string Path { get; set; }
        public bool Enabled { get; set; }
    }
}

Call the Move Type to File refactoring to move a type declaration to a new source code file.