Skip to main content
All docs
V22.2
  • CRR0047 - The type can be moved to a separate file

    • 3 minutes 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.

    See Also