Methods C#

Methods C#

Methods C#

A method is a code block that contains a series of statements. A program causes the statements to be executed by calling the method and specifying any required method arguments. In C#, every executed instruction is performed in the context of a method. The Main method is the entry point for every C# application and it is called by the common language runtime (CLR) when the program is started.

The main question in this exercise is How to change the object behavior. When we create a class we use data members to represent object state and methods to represent the object behavior.

Method Signature in C#:

Each method is declared as follows:

Return-type methodname ( Parameterslist );

For an example if we create a class User which we have used in our previous example, we can do some changes on the userInfo object. First lets remember how our User class looked like in the previous chapter:

namespace iNGENIUMWEB.Tutorial
{
    class FirstProgram
    {
        public class User
        {
            public User(string fName, string lName)
            {
                firstName = fName;
                lastName = lName;
            }

            public string firstName;
            public string lastName;
            public string email;
            public int age;
            private string password;

            public void SetPassword(string newPassword)
            {
                password = newPassword;
            }

            public string GetPassword()
            {
                return password;
            }
        }

        static void Main(string[] args)
        {
            User userInfo = new User("John", "Johnson");
            userInfo.SetPassword("password");
            Console.WriteLine("User passowrd is " + userInfo.GetPassword());
        }
    }
}

You can see that in the body of the class are defined two methods: SetPassword and GetPassword.
First method uses for setting a password for user. The return type of this method is void. We used void because we didn't want anything back from the method. You can also see that inside the round bracketts is defined a input parametar newPassword. It uses for defining a new password for our user. We use it to set a new password for our user.

The input parameters can be passed in two ways.

  • Value type
  • Reference type.

If parameters are passed as value types a new copy of it will be created and passed inside the function. If they are created as reference types, only the address of the parameters will be passed.

Maybe in some moment we will need to know the password from our current user. Now You can take a look at our second method GetPassword. Our second method is slightly different from first method. The return type of the second method is string, which is an in-built data type. The methods can also return any generic C# type or any custom types created by us.

Now we will see how we can use these two methods.

static void Main(string[] args)
 {
  User userInfo = new User("John", "Johnson");
  userInfo.SetPassword("password");
  Console.WriteLine("User passowrd is " + userInfo.GetPassword());
 }

You can see that in the first line we have a created a new object userInfo. In the second line we have set a password for our user. You can see that we called a method SetPassword("password") and passed a string parametar to this method. If you have tried to pass any other parametar beside a string You will recive an error, because you can only pass a parametar of type string to this method.

Output Parameters in Methods:

The return values in any function will be enough for any one if only one value is needed. But in case a function is required to return more than one value, then output parameters are the norm. In C# the output parameter is declared with the keyword out before the data type. A typical example is as follows.

public void CalculateUserBirthYear(ref int year, out int birthyear)
    {
        int b = year - m_old;
        Console.WriteLine("Birth year is {0}",b);
         birthyear = b;
          return;
     }

Strictly speaking there is no difference between ref and out parameters. The only difference is that the ref input parameters need an input value and the out parameters dont.

Variable arguments in C#:

The C# language supports variable arguments through a keyword called params. A typical example for the declaration of a function with variable argument signature is as follows.

Public void functionName(int a, params int[] varParam);

Method Overloading in C#

A method is considered to be an overloaded method, if it has two or more signatures for the same method name. These methods will contain different parameters but the same return types.
A simple example for an overloaded methods are:

Public void functionName(int a, params int[] varParam);
    Public void functionName(int a);

Posted by Ingenium Llc.

Ingenium Llc.

iNGENIUM Llc. is an software development company from EU which delivers a full range of custom .NET, web and mobile solutions for different business to meet partner's demand.

The Power of Imagination Makes Us Infinite

Related Posts

Comments

comments powered by Disqus