What will be the output of the following C# code? [code] class sample { public int i; public int[] arr = new int[10];

What will be the output of the following C# code?
Code:
 class sample
 {
     public int i;
     public int[] arr = new int[10];
     public void fun(int i, int val)
     {
         arr[i] = val;
     }
 }
 class Program
 {
     static void Main(string[] args)
     {
         sample s = new sample();
         s.i = 10;
         sample.fun(1, 5);
         s.fun(1, 5);
         Console.ReadLine();
     }
 }
A. sample.fun(1, 5) will not work correctly
B. s.i = 10 cannot work as i is ‘public’
C. sample.fun(1, 5) will set value as 5 in arr[1]
D. s.fun(1, 5) will work correctly
 
Answer: a
Explanation: An Object reference is required for non static field, method or property. i.e
Code:
                      sample s = new sample();
                      s.i = 10;
                      sample.fun(1, 5);
                      sample.fun(1, 5);
                      Console.ReadLine();
 

Tìm kiếm

Back
Top