C# Programming - Classes and Objects - Discussion

Discussion Forum : Classes and Objects - General Questions (Q.No. 9)
9.
Which of the following statements is correct about the C#.NET code snippet given below?
namespace IndiabixConsoleApplication
{ 
    class Sample
    { 
        public int index; 
        public int[] arr = new int[10]; 
        
        public void fun(int i, int val)
        { 
            arr[i] = val;
        }
    }
     
    class MyProgram
    { 
        static void Main(string[] args)
        {
            Sample s = new Sample(); 
            s.index = 20; 
            Sample.fun(1, 5); 
            s.fun(1, 5); 
        } 
    } 
}
s.index = 20 will report an error since index is public.
The call s.fun(1, 5) will work correctly.
Sample.fun(1, 5) will set a value 5 in arr[ 1 ].
The call Sample.fun(1, 5) cannot work since fun() is not a shared function.
arr being a data member, we cannot declare it as public.
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
16 comments Page 2 of 2.

Mdmudassir said:   1 decade ago
Shared in vb.net = static in c#.

So the D option is really confusing.

Gangadhar said:   9 years ago
The fun is not a static function. We cannot access a nonstatic method directly with the class name.

Then we'll get a compile time error.

Rishabh said:   9 years ago
Why option C is not the correct answer?

Bilal said:   8 years ago
Option D is Correct.

Error
1 An object reference is required for the non-static field, method, or property 'ConsoleApplication1.Sample.fun(int, int)'.

Xiyoz said:   8 years ago
The fun is not a static function. We cannot access a nonstatic method directly with the class name.

Then we'll get a compile time error.

Option D is Correct.

Vinay said:   8 years ago
Yes, Option D is correct. I agree.


Post your comments here:

Your comments will be displayed after verification.