Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

void vs private void in C#

Tags:

c#

private

void

In C# UI codes, when I create event methods, it gets automatically populated with

void simpleButton_click(object sender, Eventargs e)
{
}

What's the difference between this simple void and private void?


1 Answers

None, it's syntactical. By default members are private while types are internal).

Often people add private for the sake of consistency, especially when it's in a class or type that has many other members with differing access attributes, such as protected internal or public.

So the following two files are equivalent:

Implicit.cs

using System;

namespace Foo
{
    class Car : IVehicle
    {
        Car(String make)
        {
            this.Make = make;
        }

        String Make { get; set; }

        CarEngine Engine { get; set; }

        void TurnIgnition()
        {
            this.Engine.EngageStarterMotor();
        }

        class CarEngine
        {
            Int32 Cylinders { get; set; }

            void EngageStarterMotor()
            {
            }
        }

        delegate void SomeOtherAction(Int32 x);

        // The operator overloads won't compile as they must be public.
        static Boolean operator==(Car left, Car right) { return false; }
        static Boolean operator!=(Car left, Car right) { return true; }
    }

    struct Bicycle : IVehicle
    {
        String Model { get; set; }
    }

    interface IVehicle
    {
        void Move();
    }

    delegate void SomeAction(Int32 x);
}

Explicit.cs

using System;

namespace Foo
{
    internal class Car : IVehicle
    {
        private Car(String make)
        {
            this.Make = make;
        }

        private String Make { get; set; }

        private CarEngine Engine { get; set; }

        private void TurnIgnition()
        {
            this.Engine.EngageStarterMotor();
        }

        private class CarEngine
        {
            private Int32 Cylinders { get; set; }

            private void EngageStarterMotor()
            {
            }
        }

        private delegate void SomeOtherAction(Int32 x);

        public static Boolean operator==(Car left, Car right) { return false; }
        public static Boolean operator!=(Car left, Car right) { return true; }
    }

    internal struct Bicycle : IVehicle
    {
        private String Model { get; set; }
    }

    internal interface IVehicle
    {
        public void Move(); // this is a compile error as interface members cannot have access modifiers
    }

    internal delegate void SomeAction(Int32 x);
}

A summary of the rules:

  • Types (class, struct, enum, delegate, interface) defined directly in a namespace are internal by default.
  • Members (Methods, Constructors, Properties, Nested Types, Events) are private by default, with two exceptions:
    • Operator-overloads must be explicitly marked public static. You will get a compile error if you do not provide an explicit public access modifier.
    • Interface members are always public and you cannot provide an explicit access modifier.
  • In C#, both class and struct members are private by default, unlike C++ where struct is public by default and class is private by default.
  • Nothing is ever implicitly protected or protected internal in C#.
  • .NET supports "Friend Assemblies" where internal types and members are visible to code inside another assembly. C# requires the [assembly: InternalsVisibleTo] attribute to achieve this. This is not the same thing as C++'s friend feature (C++'s friend allows a class/struct to list other classes, structs and free-functions that will have access to its private members).
like image 144
Dai Avatar answered Sep 05 '25 15:09

Dai