Sir,
I am starting methods in c sharp and i have confustion with pass by value and reference.
also i get confused what passes incase of arrays.
example in the below code of Write a method that checks whether an element, from a certain position in an array is greater than its two neighbours.
- static void Main()
- {
- int[] array=new[] {1,2,3,2,5,8,7,9};
- int position = int.Parse(Console.ReadLine());
- ElementCheck(position, array);
- }
- static void ElementCheck(int newposition, int[] array)
- {
- if ((array[newposition]> array[newposition-1]) && (array[newposition]> array[newposition+1]))
- {
- Console.WriteLine("Greater");
- }
- else
- {
- Console.WriteLine("Not greater");
- }
- }
Questions are :
1. I am calling ElementCheck(position, array); but how will all the elements of array pass to the elementcheck?
Also, can we return string ...for exampe like this below:
- static void Main()
- {
- int[] array=new[] {1,2,3,2,5,8,7,9};
- int position = int.Parse(Console.ReadLine());
- ElementCheck(position, array);
- }
- static string ElementCheck(int newposition, int[] array)
- {
- if ((array[newposition]> array[newposition-1]) && (array[newposition]> array[newposition+1]))
- {
- return greater?;
- }
- else
- {
- return notgreater?;
- }
- }
This code gives me error. Why?