C# Interview Questions on structs

Q : Will the following code compile?

Ans : using System;
public class Example
{
static void Main()
{
TestStruct T = new TestStruct();
Console.WriteLine(T.i);
}
}
public struct TestStruct
{
public int i=10;
//Error: cannot have instance field initializers in structs
}
No, a compile time error will be generated stating "within a struct declaration, fields cannot be initialized unless they are declared as const or static"

Q : Can a struct have a default constructor (a constructor without parameters) or a destructor in C#?

Ans : No

Q : Can you instantiate a struct without using a new operator in C#?

Ans : Yes, you can instantiate a struct without using a new operator

Q : Can a struct inherit from another struct or class in C#?

Ans : No, a struct cannot inherit from another struct or class, and it cannot be the base of a class.

Q : Can a struct inherit from an interface in C#?

Ans : Yes

Q : Are structs value types or reference types?

Ans : Structs are value types.

Q : What is the base type from which all structs inherit directly?

Ans : All structs inherit directly from System.ValueType, which inherits from System.Object.

     
Leave a Reply
Name (required)
Email (will not be published) (required)
Web Site

List of Top Hospitals in India