real values

interview questions of dot net

Posted on

1.)  What is an Object?

Object is an instance of a class; it contains real values instead of variables. For example, let’s create an instance of class Employee called “John”.

Employee John= new Employee ();

Now we can access all the methods in the class “Employee” via object “John” as shown below.

John.setName (“XYZ”);

 

2.) What are the Access Modifiers in C#?

Different Access Modifier are – Public, Private, Protected, Internal, Protected Internal

  • Public – When a method or attribute is defined as Public, It can be accessed from any code in project. For example in the above Class “Employee”, getName(), setName() etc are public.
  • Private – When a method or attribute is defined as Private, It can be accessed by any code within the containing type only. For example in the above Class “Employee”, attributes name and salary can be accessed with in the Class Employee Only. If an attribute or class is defined without access modifiers, its default access modifier will be private.
  • Protected – When an attribute and methods are defined as protected, it can be accessed by any method in inherited classes and any method within the same class. The protected access modifier cannot be applied to class and interfaces. Methods and fields in an interface cannot be declared protected.
  • Internal – If an attribute or method is defined as Internal , Access is restricted to classes within the current project assembly
  • Protected Internal – If an attribute or method is defined as Protected Internal, Access is restricted to classes within the current project assembly and types derived from the containing class.

 

3.) Define Static Members in C#?

  • If an attributes value had to be same across all the instances of the same class, static keyword is used. For example if the Minimum salary should be set for all employees in the employee class, use the following code

 

private static double MinSalary = 30000;

 

To access a private or public attributes or methods in a class, at first an object of the class should be created. Then using that instance of class, attributes or methods can be accessed. To access a static variable, we don’t want to create an instance of the class having the static variable. We can directly refer that static variable as shown below.

double var = Employee.MinSalary ;

 

Posted By-: Vissicomp Technology Pvt. Ltd.

Website -: http://www.vissicomp.com

 

 

Advertisements