Classes and Objects Questions in C#

By
1. ) what is class in C#?                                                                                                        
A class is a user-defined data type with a template that serves to define its properties.
More Details...

2. ) How to declare a class in C#?                                                                                      
Syntax:-
Class class-name
{
variables declaration;
Method declaration;
}

                                                               OR

Class class-name
{
                                                                    //empty class
}


3. ) Which type of properties can include in C# class?                                                 
  • Indexers
  • Operators
  • Constructors
  • Destructors
4. ) What is instance variables in C#?                                                                               
Data is encapsulated in a class by placing data fields inside the body of the class definition.These variables are called instance variables.we can create the instance variables exactly the same way as we create local variables.Instance variables are called member variables.
Ex.
Class student
{
string name;          //instance variables
int age ;                //instance variables
money salary        //instance variables
}

5. ) What is an object in C#?                                                                                               
An object is a Runtime entity of any class,structure or union.
Ex.
class student
{
int x =10;
int y=20;
public void display()
{
console.WriteLine("value of x is: +x");
console.WriteLine("value of y is: +y");
student st -new student()                   //object  'st' created
st.display();          //here "st" object call the display method of student class
}
}

6. ) What is difference between object and instance?                                                  
An Instance is a declaration time entity but an Object is a Runtime entity in class ,structure and union.

7. ) What is Method and  how to declare in class?                                                         
An Method is used for manipulation the data contained in the class.Methods are always declared inside the body of the class.
class student
{
string name;
int age;
public void GetData(string s,int x)
{
name=s;
age=x;
console.writeLine("value of name is"+s);
console.writeLine("value of age is"+x);
}
}


8. ) Can we place method definition before instance variable in c#?                      
Yes.
9. ) Can we access the variables declared in other methods within same class in c#? 
Yes     --> if variables are public
No     ---> if variables are private    

10. ) What is access-specifier or modifier in c#?                                                           
More Details...

11. ) How can we create an object of the class in C#?                                                    
C# usages New operator to create the object of the class.
Syntax:
classname  objectname = new classname();
                          OR
classname objectname;
objectname =new classname();

Ex.
student st = new student();
                    OR
student st ;
st = new student();

12. ) Can we create more than one object of a class in C#?                                          
Yes, Both object are independent to each other,Means each have your copy of instance variables.

13. ) What is the syntax for accessing the members of the class in c#?                    
Syntax:
objectname. variablename
object.methodname (parameter-list);

14. ) What is constructor in c#?                                                                                         
A constructor is like a method,which is used to initialization of data member of the class,when is created.
There are some properties of constructor in c#.

  • The Name of constructor should be same as class name.
  • Constructors can be public,private or protected in  the class.
  • Constructors are automatically called ,when object of class is created.


15. ) Is constructor overloading possible in c#?                                                            
Yes.
More Details...

16. ) What is partial class in c#?                                                                                         
More Details..

17. ) What is the use of 'this' keyword in c#?                                                                 
There are following reason to use 'this' keyword in c#.
  • If  we want to refer the current member of the current class then we can use 'this' keyword to refer that member.
  • If we want to call a specific constructor of the class through another constructor of same class then we can specify 'this' keyword with that constructor.
  • We can use 'this' keyword ,when variable name and object name is same.
18. ) What is Destructor in c#?                                                                                          

Destructor is a method,it is called when an object is no more required.The name of destructor is same   as class name.It is use prefix' ~'. It is used to deallocate the memory used by resources within the class.
More Details...

19. ) Can I use nested classes ,structs,interface and enum to others in c#?            
Yes.

20. ) What is Constant members in class and its use ?                                                 
It is used to declared the data fields of the class as constants.
Ex.
Public const int size = 200;
In above Example ,member size is assigning the a value of 200 at compile time ,we can not change this value later in the program.
Constant members are implicitly static often,we can not declare them explicitly using static.
Ex.
Public static const int size = 200;
It is wrong ,it will give compile time error.

21. ) What is Read only members in c# ?                                                                         
ReadOnly are basically used to set the constant value at Runtime.Once value is assigned at Runtime then it can not change later.
Ex.
class student 
{
  Public readonly int x;
  Public static readonly int y;
Public student(int a)
{
  m = x; 
}
static student()
{
  y = 50 ;
}
} 

22. ) What is Properties  in c# ?                                                                                         
More Details...

23. ) What is Indexer  in c# ?                                                                                             
More Details...

24. ) What is difference between properties and Indexer  in c# ?                             

  • Indexer is always an instance member whereas property can be static member.
  • A get accessor of  a property corresponds to a method with no parameters,whereas a get accessor of an indexer corresponds to a method with  the same parameter list as the indexer.
  • A Set accessor of an indexer corresponds to a member with the same formal parameter list as the indexer and the parameter named value.
  • Indexer gives error if we declare a local variable with  the same name as indexer.

25. ) What is static and Non static member  in c# ?                                                     
Static Member:- Static members are not associated with the class object, so to access them ,we no need to create the object of the class.There will be one single copy of static data member in the class. All Static members are accessed by the class name only.
Non Static Member:- When we create the member of  the class,then they are Non Static  by default.Means ,to access them we need to object of class and every object will have its own copy of Non Static member.All Non Static members are accessed by the object of the class

0 comments:

Post a comment

Powered by Blogger.