Question

Possible Duplicate:
What is the difference between Public, Private, Protected, and Nothing?

I have a question : What is the difference between these method types ?

Static , Public , Internal , Protected , const , void

Sorry my question may seem awkward to professionals but i really want to understand the difference , and by the way i searched and read articles about them but they are all big and not well described , i just need a nice example for each so i could make decision each time i make a function , because i always start with private void ........

Was it helpful?

Solution

Your basic method has the following:

[access modifier?] [static?] [return type or void] [name] ([parameters?])

There's a few extra bits and pieces but that's your start.

Access Modifiers

Some of those are access modifiers which control which classes have access (can call) whatever you've put the modifier on.

// Anyone can call me
public int SomeMethod() { return 1; } 

// Only classes in the same assembly (project) can call me
internal int SomeMethod() { return 1; } 

// I can only be called from within the same class
private int SomeMethod() { return 1; }

// I can only be called from within the same class, or child classes
protected int SomeMethod() { return 1; }

Static

Static means that the method/variable is shared by all instances of the class. It can be combined with an access modifier from above.

public class Test
{
  static int a = 0;
  public int SomeMethod() { a = a + 1; return a; }
}

Test t1 = new Test();
t1.SomeMethod(); // a is now 1
Test t2 = new Test();
t2.SomeMethod(); // a is now 2

// If 'a' wasn't static, each Test instance would have its own 'a'

Void

void just means that you have a method that doesn't return anything:

public void SomeMethod() 
{ 
  /* I don't need to return anything */ 
}

const

const means that the variable cannot be modified:

const int LIFE = 42;
// You can't go LIFE = 43 now

OTHER TIPS

The keywords public, private, protected and (protected) internal, are called access modifiers and determine who can access a given class and/or its members, which helps in encapsulation and abstraction.

The storage modifier (thanks AVD) static defines a static method, property or class, const creates a constant and void indicates a return type of 'nothing'.

While developing, it's normal you start with a private void DoSomething(), and only if you can and need to access the method externally, you increase the accessibility. If you expect it to be overridden in the same namespace, you use protected. If you're writing a library to be used by another application (in a different namespace), you mark it public, and so on.

If you want it to return something at some point, you change void to the appropriate type. All of this is (including deciding when to make something static or constant) addressed in most basic OO books and tutorials, try to pick up one.

these are called Access Modifiers

public

The type or member can be accessed by any other code in the same assembly or another assembly that references it.

private

The type or member can only be accessed by code in the same class or struct.

protected

The type or member can only be accessed by code in the same class or struct, or in a derived class.

internal

The type or member can be accessed by any code in the same assembly, but not from another assembly.

protected internal

The type or member can be accessed by any code in the same assembly, or by any derived class in another assembly.

In addition to what CodeCaster said...

void is in the place where the type of the returned value is declared. Void means that there is no return type in a given method.

const indicates the type is a constant and will be defined once and then not modified.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top