There is a difference between private void, public void, public bool, public string, etc. Can someone explain this to me?

Asked

Viewed 1,058 times

3

Until now to create functions in c# it was rare to use the public bool to create functions, used more the private void and the public void. I was looking at a tutorial where he used for example:

public bool verificar (string jogador)
{
//código
}

Can you explain to me the differences?

  • Until now to create functions in c# it was rare to use if you have created several codes I imagine you should know the difference between a void and a bool right? It depends on what your method will return... a method called "veriricar" seems good to me to return a bool, according to the validation being true or talking about private and public, I think you already have an answer about this: https://answall.com/questions/23/qual-%C3%A9-a-difference%C3%A7a-between-modifiers-public-default-protected-e-private

1 answer

5

We have to understand what each particle of a method declaration means:

encapsulamento tipo_de_retorno nome_do_metodo(parâmetros)

Explaining each field:

Encapsulation: You say where this method can be accessed, for this you have public, private, protected, internal.

In the case of public is accessible by any scope;

In the case of private is accessible only within the scope of the class in which it was declared;

In the case of protected is accessible only within the scope of the class in which it was declared and its daughter classes;

In the case of internal it is accessible only by members belonging to the same Assembly package.

Return type is the type your method will return, it can be any object or primitive type, be bool, string, int, MeuObjeto, etc..

This is the one that defines that will be returned by the keyword return.

Method_name is the term you used to designate the method to be called.

Parameters are the parameters that this method will receive and work within it

An example would be:

public int soma(int a,int b)
{
    return a+b;
}

The previous method can be accessed by any part of the code and its return will be a int which represents the sum of parameters a and b.

  • 1

    Thank you for your attention, I was able to understand the differences

  • @Gonçalosousa if this answer serves as answer to your question, could mark it as an answer, please?

Browser other questions tagged

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