How many classes can I put in one code?

Asked

Viewed 213 times

1

How many "classes" I can add to my code?

Let’s say I have the following existing command block:

namespace _06_ByteBank
{
    public class Cliente
    {
        private string _cpf;

    public string Nome { get; set; }
    public string CPF
    {
        get
        {
            return _cpf;
        }
        set
        {
            // Escrevo minha lógica de validação de CPF
            _cpf = value;
        }
    }
    public string Profissao { get; set; }
  }
}

To add another "class" I would write under this block or inside it?

  • I don’t know anything that would limit putting two classes in one namespace, or using multiple namespaces in the same file

1 answer

4


As many as you want. There’s no theoretical limit. I can assure you that you will have other problems before reaching any technical limit (which will be more by available memory limit for the compiler, and we already have plenty).

The problem is that you are thinking the wrong way. You must learn to organize code. Understand the problem and make the most appropriate code possible to solve it. Learning this takes a lot of time, it takes a lot of dedication, and it’s a lot harder than people realize. Doing it anyway and getting it to work is easy, writing quality and lasting code is something much more complicated, and the observation I have is that most of the people who are starting lately aren’t learning this because they just want to see the result, no matter how. In the long run it doesn’t work.

You do not need to put another class there. You can create another file with the new class. Of course, depending on the case, you will have to learn when to put in the same file or not.

But if you want to insist you have to put the class inside the namespace, I mean, it’s probably possible to put it away, but it’s unlikely that you wanted this. You might even want to create another namespace outside. Or even nesting them.

You can repeat the namespace, it is only a surname. Even in different files all types that use the same name of namespace will be part of it.

It would be nice to learn some C nomenclature standards# to go organizing the code.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.