C# Interview Questions on structs




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.



All Questions

ASP.NET

CSharp

SQL Server

WCF

HR Interview Questions 

Didn't find what you are looking for? Search this

site, for frequently asked ASP.NET, C#, SQL Server, WCF

and HR Interview Questions









Email This

 BlogThis!

 Share to Twitter

 Share to Facebook

 Share to Google Buzz

 3 comments:

Anonymous said...

Structure have default constructor which initialize the field member to zero.



September 10, 2010 5:44 AM 

Anonymous said...

"No, 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."



Don't those two statements contradict one another?



April 21, 2011 8:11 AM 

Anonymous said...

System.ValueType does not derive from System.Object.



// Summary:

// Provides the base class for value types.

[Serializable]

[ComVisible(true)]

public abstract class ValueType

{

// Summary:

// Initializes a new instance of the System.ValueType class.

protected ValueType();



// Summary:

// Indicates whether this instance and a specified object are equal.

//

// Parameters:

// obj:

// Another object to compare to.

//

// Returns:

// true if obj and this instance are the same type and represent the same value;

// otherwise, false.

public override bool Equals(object obj);

//

// Summary:

// Returns the hash code for this instance.

//

// Returns:

// A 32-bit signed integer that is the hash code for this instance.

public override int GetHashCode();

//

// Summary:

// Returns the fully qualified type name of this instance.

//

// Returns:

// A System.String containing a fully qualified type name.

public override string ToString();

}

}

----------------

Basic C# Interview Questions on classes and structs

What do you mean by saying a "class is a reference type"?

A class is a reference type means when an object of the class is created, the variable to which the object is assigned holds only a reference to that memory. When the object reference is assigned to a new variable, the new variable refers to the original object. Changes made through one variable are reflected in the other variable because they both refer to the same data.



What do you mean by saying a "struct is a value type"?

A struct is a value type mean when a struct is created, the variable to which the struct is assigned holds the struct's actual data. When the struct is assigned to a new variable, it is copied. The new variable and the original variable therefore contain two separate copies of the same data. Changes made to one copy do not affect the other copy.



When do you generally use a class over a struct?

A class is used to model more complex behavior, or data that is intended to be modified after a class object is created. A struct is best suited for small data structures that contain primarily data that is not intended to be modified after the struct is created.





List the 5 different access modifiers in C#?

1. public

2. protected

3. internal

4. protected internal

5. private



If you donot specify an access modifier for a method, what is the default access modifier?

private



Classes and structs support inheritance. Is this statement true or false?

False, Only classes support inheritance. structs donot support inheritance.



If a class derives from another class, will the derived class automatically contain all the public, protected, and internal members of the base class?

Yes, the derived class will automatically contain all the public, protected, and internal members of the base class except its constructors and destructors.



Can you create an instance for an abstract class?

No, you cannot create an instance for an abstract class.



How do you prevent a class from being inherited by another class?

Use the sealed keyword to prevent a class from being inherited by another class.



Classes and structs can be declared as static, Is this statement true or false?

False, only classes can be declared as static and not structs.



Can you create an instance of a static class?

No, you cannot create an instance of a static class.



Can a static class contain non static members?
No, a static class can contain only static members.

No comments:

Post a Comment