blogs

miércoles, 3 de junio de 2015

Access Levels of the methods

A method is a class member that contains a code block that represents an action:

  • All executable code belongs to a method
  • A C# application must have at least one method
  • Methods can be defined for private use by a type, or  for public use by other types

The Main method defines the starting point for an application.

A method contains:

1. The method specification

  • Name:Start with letter or underscore. Can contain letters, underscore and numeric characters
  • Parameters: Created when the method runs. Populated with values when the method is called
                             bool    LockReport(string reporName, string username){
                                             //code
                               }
                         The parameters is separated by comma. If the method don't take a parameters,we                                    should specified empty parameter less.
                            string MyMethod(){
                                  //code
                            }

  • Return type: All methods have a return type. 
                           string MyMethod(){
                                  //code
                                 return "Hello";
                            }
 If we define a return type, we should return a value within  the method, expression in return statement and method type must match.
                          The method don't return is void. 
                        void createReport(string reportname)
                         {
                                //code
                            }

  • Accessibility: public, private, protected, internal
private: This only be Accessible within the class.Access is limited to the containing type
public:  Access is not restricted
protected: Access is limited to the containing class or types derived from the containing class
internal: Access is limited to the current assembly.

2. The method body: Implement the code, the body is always enclosed in braces.we defined the variables. The variable always exists while the method is running, when the method is finished, they are disappear

Creating and calling overloaded Methods
An overloaded methods:

  • Has the same name as an existing method
  • Should perform the same operation as the existing method
  • Uses different parameters to perform the operation

The compiler invokes the version that is appropriate to the arguments that are specified the method is called

Refactoring Code into a method
1. Select the code that we want to extract to method, Right click, point to Refactor, and then click Extract Method
2. In the  Extract Method dialog bok, in the New method name  box, type a name for the method, and then click  Ok.








Static Method:
when we have a static method does not need create a instance of class to access their methods.
We only need the name to class and call the method.
For example:


Access level

Private:

Public:



protected:
if we have a devised class, we can look that this is allowed.


internal:



 Create a new test Unit and reference the namespace


Change the access level 





No hay comentarios:

Publicar un comentario