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.

Sachin said:   1 decade ago
Please understand me what is the logic behind it?

Sumesh SG said:   1 decade ago
Sample.fun(1, 5); -- This line shows compilation error; because Sample is nonstatic class.

Muppet said:   1 decade ago
Sample.fun(1, 5) = You can't access a non-static method in a static context.

Other than that the only other one that is right is B.

I'm with @Yao.

Rahul said:   1 decade ago
S is a object of class Sample. So the as per the property of an object of any class, it call its member functions using object.fuction()name syntax. Hope this will help.
(1)

Yao said:   1 decade ago
fun is not a static function, so this program is not even compilable.

Abi said:   1 decade ago
Please anybody could explain me. I couldnt understand.


Post your comments here:

Your comments will be displayed after verification.