HI WELCOME TO SIRIS

C# Interview Questions on structs

Leave a Comment

Will the following code compile? 

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"

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

Can you instantiate a struct without using a new operator in C#?Yes, you can instantiate a struct without using a new operator

Can a struct inherit from another struct or class in C#? 
No, a struct cannot inherit from another struct or class, and it cannot be the base of a class.

Can a struct inherit from an interface in C#?Yes

Are structs value types or reference types? 
Structs are value types.

What is the base type from which all structs inherit directly?All structs inherit directly from System.ValueType, which inherits from System.Object.

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.