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 1 of 2.

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

Ninad said:   5 years ago
Option D is correct because we can't call a method like class name dot method name when it is not a static class.

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

Bilal said:   7 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:   7 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.

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

Gangadhar 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.

Mdmudassir said:   9 years ago
Shared in vb.net = static in c#.

So the D option is really confusing.

K2u2007 said:   9 years ago
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.

Nicholas Mahbouby said:   10 years ago
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".


Post your comments here:

Your comments will be displayed after verification.