harish reddy

harish reddy

  • NA
  • 162
  • 33.9k

Methods in C Sharp

Feb 9 2018 8:40 AM
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.
  1. static void Main()  
  2. {  
  3. int[] array=new[] {1,2,3,2,5,8,7,9};  
  4. int position = int.Parse(Console.ReadLine());  
  5. ElementCheck(position, array);  
  6. }  
  7. static void ElementCheck(int newposition, int[] array)  
  8. {  
  9. if ((array[newposition]> array[newposition-1]) && (array[newposition]> array[newposition+1]))  
  10. {  
  11. Console.WriteLine("Greater");  
  12. }  
  13. else  
  14. {  
  15. Console.WriteLine("Not greater");  
  16. }  
  17. }  
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:
  1. static void Main()  
  2. {  
  3. int[] array=new[] {1,2,3,2,5,8,7,9};  
  4. int position = int.Parse(Console.ReadLine());  
  5. ElementCheck(position, array);  
  6. }  
  7. static string ElementCheck(int newposition, int[] array)  
  8. {  
  9. if ((array[newposition]> array[newposition-1]) && (array[newposition]> array[newposition+1]))  
  10. {  
  11. return greater?;  
  12. }  
  13. else  
  14. {  
  15. return notgreater?;  
  16. }  
  17. }  
This code gives me error. Why?

Answers (2)