Classes and Structs:
1. Some points to remember for structs?
a. Within a struct declaration, fields cannot be initialized unless they are declared as const or static.
b. A struct cannot declare a default constructor (a constructor without parameters) or a destructor.
c. Structs are copied on assignment. When a struct is assigned to a new variable, all the data is copied, and any modification to the new copy does not change the data for the original copy. This is important to remember when working with collections of value types such as Dictionary<string, myStruct>.
d. Structs are value types and classes are reference types.
e. Unlike classes, structs can be instantiated without using a new operator.
f. Structs can declare constructors that have parameters.
g. A struct cannot inherit from another struct or class, and it cannot be the base of a class. All structs inherit directly from System.ValueType, which inherits from System.Object.
h. A struct can implement interfaces.
i. A struct can be used as a nullable type and can be assigned a null value.
j. It is an error to define a default (parameterless) constructor for a struct. It is also an error to initialize an instance field in a struct body. You can initialize struct members only by using a parameterized constructor or by accessing the members individually after the struct is declared. Any private or otherwise inaccessible members can be initialized only in a constructor.
2. Can we initialize Stucts with new keyword?
a. When you create a struct object using the new operator, it gets created and the appropriate constructor is called. Unlike classes, structs can be instantiated without using the new operator. In such a case, there is no constructor call, which makes the allocation more efficient. However, the fields will remain unassigned and the object cannot be used until all of the fields are initialized.
3. What are static classes?
a. Contains only static members.
b. Cannot be instantiated.
c. Is sealed.
d. Cannot contain Instance Constructors.
4. Where does a call to static method executed?
a. A call to a static method generates a call instruction in Microsoft intermediate language (MSIL), whereas a call to an instance method generates a callvirt instruction, which also checks for a null object references
5. What is readOnly Field in C#?
a. A field can be declared readonly. A read-only field can only be assigned a value during initialization or in a constructor. Creating a static read only makes a constant field.
6. What are constant?
a. Constants are immutable values which are known at compile time and do not change for the life of the program. Constants are declared with the const modifier.
7. Can we have const enum, methods etc.
a. NO
8. Can we initialize a constant that can refer to another constant if it does not create a circular reference?
a. Yes
Constructors:
1. A constructor that takes no parameters is called a default constructor
2. structs cannot contain an explicit default constructor because one is provided automatically by the compiler.
3. A constructor can use the base keyword to call the constructor of a base class.
5. What is a private constructor?
a. A private constructor is a special instance constructor. It is generally used in classes that contain static members only.
b. If a class has one or more private constructors and no public constructors, other classes (except nested classes) cannot create instances of this class.
6. What is the Default access level for a constructor?
a. Private
7. What is a static constructor?
a. It is called automatically before the first instance is created or any static members are referenced.
b. A static constructor does not take access modifiers or have parameters.
c. A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.
d. A static constructor cannot be called directly.
e. The user has no control on when the static constructor is executed in the program.
f. A typical use of static constructors is when the class is using a log file and the constructor is used to write entries to this file.
g. Static constructors are also useful when creating wrapper classes for unmanaged code, when the constructor can call the LoadLibrary method.
h. If a static constructor throws an exception, the runtime will not invoke it a second time, and the type will remain uninitialized for the lifetime of the application domain in which your program is running.
Properties:
1. Can we pass properties as reference.
a. No Properties cannot be passed as reference.
2. How to hide the base class property by derived class property?
a. Use the ‘new’ keyword. If you don’t specify this it will generate a warning saying that the base class member being hidden by derived class.
Access Modifiers
1. Following are the access modifiers:
a. Public: The type or member can be accessed by any other code in the same assembly or another assembly that references it.
b. Private: The type or member can be accessed only by code in the same class or struct.
c. Protected: The type or member can be accessed only by code in the same class or struct, or in a class that is derived from that class.
d. Internal: The type or member can be accessed by any code in the same assembly, but not from another assembly.
e. Protected internal: The type or member can be accessed by any code in the assembly in which it is declared, or from within a derived class in another assembly. Access from another assembly must take place within a class declaration that derives from the class in which the protected internal element is declared, and it must take place through an instance of the derived class type.
2. Default Access Modifiers for Class or Struct?
a. Internal
Interfaces
1. Properties of Interface?
a. An interface is like an abstract base class: any non-abstract type that implements the interface must implement all its members.
b. An interface cannot be instantiated directly.
c. Interfaces can contain events, indexers, methods, and properties.
d. Interfaces contain no implementation of methods.
e. Classes and structs can implement more than one interface.
f. An interface itself can inherit from multiple interfaces.
2. Can Interface contain Static Members?
a. An interface cannot contain constants, fields, operators, instance constructors, destructors, or types. It cannot contain static members.
3. Can Interface members be defined as Private or protected?
a. Interfaces members are automatically public, and they cannot include any access modifiers.
4. Can a static class implement an Interface?
a. No, Static classes cannot implement an Interface.
a. Then implementing that member on the class will cause both interfaces to use that member as their implementation.
interface IControl
{
void Paint();
}
interface ISurface
{
void Paint();
}
class SampleClass : IControl, ISurface
{
// Both ISurface.Paint and IControl.Paint call this method.
public void Paint()
{
}
}
b. If the two interface members do not perform the same function.This is accomplished by naming the class member with the name of the interface and a period.
void IControl.Paint()
{
System.Console.WriteLine("IControl.Paint");
}
void ISurface.Paint()
{
System.Console.WriteLine("ISurface.Paint");
}
6. How to implement Indexer in Interface?
// Indexer on an interface:
public interface ISomeInterface
{
// Indexer declaration:
int this[int index]
{
get;
set;
}
}
// Implementing the interface.
class IndexerClass : ISomeInterface
{
private int[] arr = new int[100];
public int this[int index] // indexer declaration
{
get
{
// The arr object will throw IndexOutOfRange exception.
return arr[index];
}
set
{
arr[index] = value;
}
}
}
No comments:
Post a Comment