-1

I can not understand the concept of a method that doesn't have access modifiers. What are they for? What is the use to not put access modifiers?

ChrisSemenov
  • 3
  • 1
  • 1

5 Answers5

2

A method doesn't have access modifiers written means it has a default modifier: private

shingo
  • 18,436
  • 5
  • 23
  • 42
0

In .NET, methods have default access modifier so you don't always have to specify them explicitly but it's considered a very bad practice not to explicitly specify them. See here for the default access modifier for different methods.

dragonfly02
  • 3,403
  • 32
  • 55
0

All types and type members have an accessibility level, which controls how they can be used, the following tutorial may help you to get a better grasp of C# modifiers.

C# Programming Guide

abestrad
  • 898
  • 2
  • 12
  • 23
0

if there is no access modifier written with a method it means its private by default This is a public Mehtod

public void Yourmethod()
{
}

This is a private Method by default

void Yourmethod()
{
}
0

A class member without access modifier is private. I.e., it is only visible within the class itself.

Example:

public class Test
{
    public void Hello()
    {
        Print("Hello");
        Print(" ");
        Print("World");
    }

    void Print(string s)
    {
        Console.Write(s);
    }
}

In this example the Print method is not public, but can be called from within a method that is public.

var test = new Test();
test.Hello(); // Works.
test.Print("bye"); // Not possible - compiler error!

I prefer to explicitly declare the members as private and to write the private modifier, but this is up to you:

private void Print(string s)
{
    Console.Write(s);
}
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188