4

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?

Yu Hao
  • 119,891
  • 44
  • 235
  • 294

2 Answers2

14

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).
Dai
  • 141,631
  • 28
  • 261
  • 374
  • 1
    [Here is the page from the MSDN](http://msdn.microsoft.com/en-us/library/ms173121.aspx) on this topic, it lists what the defaults are if you do not put the access modifier on (For classes it could be `private` or `internal` depending on where the class is declared) – Scott Chamberlain Dec 17 '13 at 02:46
  • 4
    important to note, This isn't true for everything. Classes have default access modifiers of internal. – The Internet Dec 17 '13 at 02:47
-4

void means to identified this block of code or procedure as method or it won't return any values. If you see any types rather than void means that the blocked of code or procedure is a function or property

this is method

private void DoSomething()
{
...code
}

this is function

private int DoSomething()
{
..code
return 1
}

private means the method, function, or property is not accessible to outside the class but can be invoke inside the class itself

public means the method, function, or property is accessible to outside the class and can also be invoke inside the class itself

Jade
  • 2,972
  • 1
  • 11
  • 9
  • 1
    This doesn't answer the question. – Harrison Dec 17 '13 at 03:13
  • @Harrison, His question is What's the difference between this simple void and private void? Read this from access modifier (Private) http://msdn.microsoft.com/en-us/library/st6sy9xe.aspx and for void see this http://msdn.microsoft.com/en-us/library/yah0tteb.aspx – Jade Dec 18 '13 at 01:38
  • The question is about explicitly using the private keyword or not (in which case for a method it's still private.) Your answer is about accessibility based on private vs. public and return value. – Harrison Dec 18 '13 at 01:48
  • Ok I won't argue you with that, but for me if you understand what it does then the difference/s will also be known. – Jade Dec 18 '13 at 01:54