none
Best class name RRS feed

  • Question

  • hi

    i have a class (class2) that helps to use another class (class1).

    what is the best name for class2. 

    Monday, February 10, 2020 1:39 PM

Answers

  • Hello,

    All classes should have a meaningful name rather than default names e.g. for reading lines from a file and processing the lines perhaps the class name might be FileOperations and there might be a helper class which only the FileOperation class uses we could have a class named FileBase.

    Both classes are shown together but should be separate files.

    public class FileBase
    {
        public string FileName;
    
        public FileBase()
        {
            FileName = Path.Combine(
                AppDomain.CurrentDomain.BaseDirectory, 
                "SomeFile.txt");
        }
    
        public List<string> ReadFile()
        {
            return File.ReadAllLines(FileName).ToList();
        }
    }
    
    public class FileOperations : FileBase
    {
        public void ProcessFile()
        {
            foreach (var line in ReadFile())
            {
                // do something
            }
        }
    }

    Or

    public class FileHelper
    {
        public string FileName;
    
        public FileHelper()
        {
            FileName = Path.Combine(
                AppDomain.CurrentDomain.BaseDirectory, 
                "SomeFile.txt");
        }
    
        public List<string> ReadFile()
        {
            return File.ReadAllLines(FileName).ToList();
        }
    }
    
    public class FileOperations
    {
        private FileHelper _fileBase = new FileHelper();
        public void ProcessFile()
        {
            foreach (var line in _fileBase.ReadFile())
            {
                // do something
            }
        }
    }


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by essamce Saturday, May 9, 2020 7:03 PM
    Monday, February 10, 2020 2:23 PM
    Moderator

All replies

  • Well it depends on what the 1st class is and what it does.

    For example class that helps "Painter" class could be "Brush".

    You need to describe your classes more. At least what they supposed to do.


    • Edited by MasaSam Monday, February 10, 2020 2:01 PM
    Monday, February 10, 2020 2:01 PM
  • Hello,

    All classes should have a meaningful name rather than default names e.g. for reading lines from a file and processing the lines perhaps the class name might be FileOperations and there might be a helper class which only the FileOperation class uses we could have a class named FileBase.

    Both classes are shown together but should be separate files.

    public class FileBase
    {
        public string FileName;
    
        public FileBase()
        {
            FileName = Path.Combine(
                AppDomain.CurrentDomain.BaseDirectory, 
                "SomeFile.txt");
        }
    
        public List<string> ReadFile()
        {
            return File.ReadAllLines(FileName).ToList();
        }
    }
    
    public class FileOperations : FileBase
    {
        public void ProcessFile()
        {
            foreach (var line in ReadFile())
            {
                // do something
            }
        }
    }

    Or

    public class FileHelper
    {
        public string FileName;
    
        public FileHelper()
        {
            FileName = Path.Combine(
                AppDomain.CurrentDomain.BaseDirectory, 
                "SomeFile.txt");
        }
    
        public List<string> ReadFile()
        {
            return File.ReadAllLines(FileName).ToList();
        }
    }
    
    public class FileOperations
    {
        private FileHelper _fileBase = new FileHelper();
        public void ProcessFile()
        {
            foreach (var line in _fileBase.ReadFile())
            {
                // do something
            }
        }
    }


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by essamce Saturday, May 9, 2020 7:03 PM
    Monday, February 10, 2020 2:23 PM
    Moderator
  • Hi essamce,

    Thank you for posting here.

    In general, naming according to the role of the class will be a good choice, but it should be noted that naming needs to follow the C# Coding Standards and Naming Conventions.

    Also, please do not use keywords in C# as class names, this will cause unnecessary trouble.

    Hope this could be helpful.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, February 11, 2020 1:48 AM