Is "private" in classes in C # optional?

23

What is the difference between private string abc = ""; and string abc = ""; ? Is there a difference or is it just the writing 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 = "";
}
    
asked by isaque 12.09.2017 в 22:24
source

4 answers

23

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

About not using any access modifiers, as in your example: in C # this is equivalent to declaring 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 members of classes and structs, including nested classes and structs, is private by default .)

That is, it's the same. As a general rule, if you do not indicate what kind of access the member can have, it will be considered as restrictive as possible. However, this may not be clear to anyone reading your code. With this in mind, it is always recommended to explicitly use the access modifier.

    
answered by 12.09.2017 / 22:47
source
16

In fact, using private or omitting it has no practical difference , since default modifier of classes and structures in C # is private.

Explicit use of keyword private denotes intent to use the access modifier more clearly.

private String texto = "";

The snippet above may seem redundant however this helps the reader of your code understand it with greater insight and ease. Who will read your code can program in other languages that have other patterns of access modifiers and so on.

All this to make things explicit is not limited to language. In the "The Zen Of Python" , a collection of Python programming principles, is defined:

  

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

Another point is to have a code writing pattern in your project, creating or adopting conventions.

    
answered by 12.09.2017 в 23:56
15

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

There are discussions whether this should be the default or even whether there should be a default. The fact is that it was adopted like this.

Just be careful that it works right. You did well to ask to see if it was not just a coincidence.

    
answered by 13.09.2017 в 04:48
3

Using a private class makes sense if it is an inner class of another class, ie:

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 Mundial class inside it is private. Therefore, for any other class, this Mundial practically does not 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 must write a class to serialize files. You can encapsulate the file in a second class that implements IDisposable, and since only the serializer class must use the file class, you can write the file class inside the serializer class and private. This ensures that the inner class will only be used by who should be used.

    
answered by 19.10.2017 в 19:43