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);
}
}
}
Discussion:
16 comments Page 1 of 2.
K2u2007 said:
1 decade 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.
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.
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)
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.
Then we'll get a compile time error.
Option D is Correct.
Nicholas Mahbouby said:
1 decade 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".
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)'.
Error
1 An object reference is required for the non-static field, method, or property 'ConsoleApplication1.Sample.fun(int, int)'.
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.
Other than that the only other one that is right is B.
I'm with @Yao.
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.
Then we'll get a compile time error.
Hatuey said:
4 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:
6 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.
(1)
Sumesh SG said:
1 decade ago
Sample.fun(1, 5); -- This line shows compilation error; because Sample is nonstatic class.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers