methods.md - brainchildservices/curriculum GitHub Wiki

Slide 1

C# Methods

A method is a group of statements that together perform a task.
Every program has a main method. In a console application, executions start in the Main method. Why use methods? To reuse code: define the code once, and use it many times.

To use a method, you need to −

  • Define the method
  • Call the method

Method definition:

  AccessSpecifier ReturnType MethodName(ParameterList)  
  {  
     Method Body  
  }

Slide 2

Access Specifier − This determines the visibility of a variable or a method from another class. Eg: Public, Private, etc Return type − A method may return a value. The return type is the data type of the value the method returns. If the method is not returning any values, then the return type is void.

Method name − Method name is a unique identifier and it is case sensitive.

Parameter list − Enclosed between parentheses, the parameters are used to pass and receive data from a method. Parameters are listed as

  DataType variablaname 

Slide 3

Method body − This contains the set of instructions needed to complete the required activity.

image

Method Signature
Method Signature --> Parameters

  1. Number of parameters
  2. Order of parameters
  3. Type of parameters

Slide 4

Calling Methods
If the method is not static, we can call the method using the format.

  objectname.Methodname(arguments); 

Writing a method to check if a number is prime or not:
https://dotnetfiddle.net/QFOsrJ

Some types of methods based on return type and parameters:

  1. Returun type void:
  2. Return type int:
  3. Return trype string
  4. Return type boolean
  5. Return type double
  6. 2 integer parameters
  7. 2 string parameters
  8. 1 string and 1 int parameter
  9. 1 int and 1 string parameter

Slide 5

If the method has a return value:
While calling the method:

   returntype variableName= object.MethodName(parameterValues);  

If the method doesn't have any return values:

   object.MethodName(parametervalues)  

Slide 6

Exercise:

Q1. Create a class Printer. Inside class Printer create a method Welcome that prints "Welcome to BrainChild". Create another Method GoodDay that prints "Have a nice day". Call both the methods in Main method.

Q2. Create a class Printer. Inside class Printer create a method that takes one string as a parameter and prints "Welcome George" where George is parameter value entered by user when it calls the method. Call both the methods in Main method.

Q3. Create a class calculator. Inside class calculator, create a method Sum. The Sum method takes two integer parameters and returns an integer. Call both the methods in Main method to print the result.

Slide 7

Q4. Write a Class Calculator. Inside class calculator create a method Greater which returns an integer. Greater has two int parameters. It checks which of the two number is greater and returns the greater value. Call the method Greater in Main method, Print the value returned by Method Greater.

Q5. Write a Class Calculator. Inside class calculator create a method PrimeChecker which returns a bool value. PrimeChecker has one int parameter. It checks if the input parameter is prime or not and returns true if its prime otherwise returns false. Call the method PrimeChecker in Main method, Print the value returned by Method PrimeChecker .

Q6. Write a Class Calculator. Inside class calculator create a method PrimeChecker which returns a bool value. PrimeChecker has one int parameter. It checks if the input parameter is prime or not and returns true if its prime otherwise returns false. In main method, declare an int array. Allow user to input the size of array, and the numbers into the array. Use the method PrimeChecker to check if the numbers stored in the array are prime or not and count the number of prime numbers and display the count.

Slide 8

Q7. Write a class ArrayManipulator. Inside class ArrayManipulator declare a method Printer that returns void, has three parameters, an integer array, starting index and ending index. The function of Printer method is to print the elements of integer array from starting index to the ending index. In Main method, declare an integer array, array1 and enter elements to it. Call the Printer method from class ArrayManipulator and pass array1 and two indexes to the method.

Q8. Write a class ArrayManipulator. Inside class ArrayManipulator declare method Printer that returns void, has three parameters, an integer array, starting index and ending index. The function of Printer method is to print the elements of integer array from starting index to the ending index. Inside class ArrayManipulator declare another method Reverse that returns an int[], has three parameters, an integer array, starting index and ending index. The function of Reverse Method is to reverse the elements of array received from starting index to the ending index store to another array and return the reversed array. In Main method, declare an integer array, array1 and enter elements to it. Call the Reverse method from class ArrayManipulator and pass array1 and two indexes to the method. Save the value returned to array2 Call the Printer method from class ArrayManipulator and pass array2 and two indexes to the method to print the reversed array

Slide 9

Q9. Write a class ArrayManipulator. Inside class ArrayManipulator declare method Printer that returns void, has three parameters, an integer array, starting index and ending index. The function of Printer method is to print the elements of integer array from starting index to the ending index. Inside class ArrayManipulator declare another method Scanner that returns void, has 2 parameters, an integer array, an integer to store how many elements to be saved into the array. The function of Scanner method is to enter elements into the array. In Main method, declare an integer array, array1. Call Scanner method from class ArrayManipulator and pass array1 and a number(the number of elements to be stored). Call the Printer method from class ArrayManipulator and pass array2 and two indexes to the method to print the array

Slide 10

Viva What is class? What is an object?
What is a field?

How to create an object?
How to instantiate a class?
How to create instance of a class?

What is OOPS?
What is polymorphism?
What is encapsulation?
What is Abstraction?
What is Inheritance?--->Base class/Parent class & Derived class/Child Class

What is a method?
What is method return type?
What do you mean by defining a method?
What do you mean by calling a method?
What are parameters?
Does order of parameter matter?
What is method signature?