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
?
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
?
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:
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);
}
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:
class
, struct
, enum
, delegate
, interface
) defined directly in a namespace
are internal
by default.private
by default, with two exceptions:
public static
. You will get a compile error if you do not provide an explicit public
access modifier.public
and you cannot provide an explicit access modifier.class
and struct
members are private
by default, unlike C++ where struct
is public
by default and class
is private
by default.protected
or protected internal
in C#.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).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