For methods with no scope specified in class C#

Asked 2 years ago, Updated 2 years ago, 41 views

C# Getting Started

public class Sample
{

  public void Hoge(){} 

  void Fuga(){} 

}

What is the difference between Sample.Hoge() and Sample.Fuga() in the above code?
What kind of treatment do you take when there is no public or private?
Also, when do I need to write like this?

c#

2022-09-30 16:18

2 Answers

Fuga() is treated as private.
Accessibility Level (C#Reference)

If no access qualifier is specified in the member declaration, the default accessibility (private is defaulted from the linked table) is used.

I think it's just a matter of preference rather than necessity. (I don't want to write one by one, it's clear that it's private.)


2022-09-30 16:18

If you omit the method, it will be treated the same as private.In other words, it has the same meaning as below.

private void Fuga(){}

I think MSDN will be helpful for more information.

There are probably many reasons to omit it, but in the case of Unity, private is already omitted in the C# script model printed by Unity, so I often see how to omit private in Unity project scripts.


2022-09-30 16:18

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.