C# Programming - Classes and Objects - Discussion

Discussion :: 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); 
        } 
    } 
}

[A]. s.index = 20 will report an error since index is public.
[B]. The call s.fun(1, 5) will work correctly.
[C]. Sample.fun(1, 5) will set a value 5 in arr[ 1 ].
[D]. The call Sample.fun(1, 5) cannot work since fun() is not a shared function.
[E]. arr being a data member, we cannot declare it as public.

Answer: Option B

Explanation:

No answer description available for this question.

Abi said: (Feb 18, 2012)  
Please anybody could explain me. I couldnt understand.

Yao said: (Oct 26, 2012)  
fun is not a static function, so this program is not even compilable.

Rahul said: (Feb 26, 2013)  
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.

Muppet said: (May 11, 2013)  
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.

Sumesh Sg said: (Nov 8, 2013)  
Sample.fun(1, 5); -- This line shows compilation error; because Sample is nonstatic class.

Sachin said: (Apr 30, 2014)  
Please understand me what is the logic behind it?

Nicholas Mahbouby said: (May 2, 2014)  
I agree with @Yao and @Muppet. Sample fun(1, 5) results in a compilation error "An object reference is required for the non-static field, method, or property".

K2U2007 said: (Oct 2, 2014)  
Understanding static.

There will be times when you will want to define a class member that will be used independently of any object of that class. Normally, a class member must be accessed through an object of its class, but it is possible to create a member that can be used by itself, without reference to a specific instance. To create such a member, precede its declaration.

With the keyword static. When a member is declared static, it can be accessed before any objects of its class are created and without reference to any object. You can declare both methods and variables to be static. The most common example of a static member is main() , which is declared static because it must be called by the operating system when your program begins.

Outside the class, to use a static member, you must specify the name of its class followed by the dot operator. No object needs to be created. In fact, a static member cannot be accessed through an object reference. It must be accessed through its class name. For example, if you.

Want to assign the value 10 to a static variable called count that is part of a class called Timer, use this line:

Timer.count = 10;.

This format is similar to that used to access normal instance variables through an object,

Except that the class name is used. A static method can be called in the same way-by use of the dot operator on the name of the class.

Mdmudassir said: (Nov 1, 2014)  
Shared in vb.net = static in c#.

So the D option is really confusing.

Gangadhar said: (May 12, 2016)  
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: (Aug 4, 2016)  
Why option C is not the correct answer?

Xiyoz said: (Mar 10, 2017)  
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.

Bilal said: (Mar 10, 2017)  
Option D is Correct.

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

Vinay said: (Jul 25, 2017)  
Yes, Option D is correct. I agree.

Ninad said: (Jun 16, 2019)  
Option D is correct because we can't call a method like class name dot method name when it is not a static class.

Hatuey said: (Sep 2, 2021)  
Compilation error (line 20, col 13): An object reference is required for the non-static field, method, or property 'Sample.fun(int, int)'.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.