Is the use of "private" in C# classes optional?

Asked

Viewed 1,016 times

24

What’s the difference between private string abc = ""; and string abc = "";? Is there any difference or is it just the way you write that changes?

I did a test with and without the private and saw no difference, it worked equally.

Example:

public class c {
   string texto = "";
}

and

public class c {
    private string texto = "";
}
  • 6
  • 2

    I believe that the question is not duplicate, after I read the question better I understood the statement, I took the opportunity and edited the title to avoid confusion.

  • 1

    If you do not put anything, the compiler puts the private for you. That is, of the same.

  • @There is no reason to be bored with this, we are human, we all made mistakes, even the most experienced of the site has this right, we are not machines, just come and talk, outside that not only who closed failed, you could also have made a better/intuitive title. Who closed it wrong, who created the question wrong, all made mistakes, what matters is to arrive and talk and in your case edit to clarify better. Talking we’ll all come.

  • @Guilherme Nascimento De boa! (-:

4 answers

25


I do not know if it is clear to you what this modifier does. I recommend reading What is the difference between public, default, protected and private modifiers?, that you have indicated in the comments. There explains well. Basically, declaring private, its property or method is not seen or modified from outside the class where it is declared. How public, it can be seen from outside. How protected, can only be seen from objects inherited from the current class.

About not using any access modifier, as in your example: in C# this is equivalent to declare as private (in the case of properties or methods):

The access level for class Members and struct Members, including nested classes and structs, is private by default.
(The access level for class members and structs, including nested classes and structs, is private by default.)

That is, it gives anyway. As a general rule, if you do not indicate what type of access the member may have, it will be considered as restrictive as possible. However, this may not be clear to anyone reading your code. Thinking about it, it is always recommended to explicitly use the access modifier.

  • 1

    Just one detail: the default access modifier is private for members of types, but for types itself is internal.

17

In fact wear private or omit it no practical difference some, since the Standard class and structure modifier in C# is the private.

The explicit use of keyword private denotes intention to use the access modifier more clearly.

private String texto = "";

The above excerpt may seem redundant but it helps the reader of your code understand it more intelligently and easily. Who will read your code can program in other languages that have other access modifier patterns and so on.

This whole thing of making things explicit is not very limited to language. No "The Zen Of Python", a collection of principles of Python programming, is defined:

Explicit is Better than implicit. (explicit is better than implicit)

Another point is that you have a code writing standard in your project, creating or adopting conventions.

16

Only complementing the answers that are correct and already answer well what was asked, contrary to what many people imagine, the types, ie classes, structures, delegates, enumerations, etc. are internal (internal) by default.

There are discussions as to whether this should be the standard or even whether there should be a standard. The fact is that it was thus adopted.

Just be careful with the consideration that working indicates you’re right. You were right to ask to see if it wasn’t just a coincidence.

3

The use of a private class makes sense if it is an inner class of another class, i.e.:

public class Palmeiras
{
    public int mundiais
    {
        get
        {
            return Palmeiras.Mundial.Contador;
        }
    }

    private class Mundial
    {
        private static int Contador = 0;
    }
}

Note that while Palmeiras is a public class, the class Mundial within it is private. Therefore, for any other class, this Mundial practically doesn’t exist.

Joking aside, there are scenarios in which having an inner class that no one else can access can be useful. For example, suppose you write a class to serialize files. You can encapsulate the file in a second class that implements Idisposable, and since only the serializer class should use the file class, you can write the file class within the serializer and private class. Thus you ensure that the innermost class will only be used by whom it should be used.

  • 1

    It would look even better if Contador were a constant

Browser other questions tagged

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