What are the similar features between class and structure in .NET?

You would have heard and experienced several differences between class and structure in .NET. But do you know that several similarities exist between the two. If not, then this article is the right place to learn them. Listed below are the similarities between class and structure:



• Constructors, Properties, Methods, Fields, Enumerations, Constants, and Events are all eligible members of both Class as well as Structure. Each of these members of a Class or Structure can have its own access modifier. Even if a Class/Structure is public, you can make its member to be of private.

• Both Class and Structure are allowed to implement an interface. Here is an example to demonstrate it:
namespace Application1 {
public interface sampleInterface {
void displayMsg();
}
public class sampleClass : sampleInterface {
public void displayMsg() {
Console.WriteLine("Executing displayMsg from sampleClass");
}
}
public struct sampleStruct : sampleInterface {
public void displayMsg() {
Console.WriteLine("Executing displayMsg from sampleStruct");
}
}
public class testClass {
public static void Main() {
sampleClass classObj = new sampleClass();
classObj.displayMsg();
sampleStruct structObj = new sampleStruct();
structObj.displayMsg();
Console.ReadLine();
}

}
}
Output of this code will be:

Executing displayMsg from sampleClass
Executing displayMsg from sampleStruct

• Both Class and Structure contain a default constructor with no arguments. In the above example, you create instance of Class and Structure using the following lines of code:
sampleClass classObj = new sampleClass();
sampleStruct structObj = new sampleStruct();

When you specify new sampleClass() or new sampleStruct(), you are calling the default
no-argument constructor of Class/Structure.

• Unlike Class, you cannot explicitly define a no-argument constructor in a Structure. However both Structure and Class can define constructor with arguments as shown in the example below:

namespace Application1 {
public class sampleClass {
public sampleClass() {
Console.WriteLine(“Calling No-Argument Constructor of
sampleClass”);
}
public sampleClass(int data) {
Console.WriteLine("Constructor of sampleClass:{0}",data);
}
}
public struct sampleStruct {
public sampleStruct(int data) {
Console.WriteLine("Constructor of sampleStruct:{0}",data);
}
}
public class testClass {
public static void Main() {
sampleClass classObj = new sampleClass(100);
sampleStruct structObj = new sampleStruct(10);
Console.ReadLine();
}
}
}

Output of this code will be:

Calling No-Argument Constructor of sampleClass
Constructor of sampleClass:100
Constructor of sampleStruct:10

If you try to explicitly define a no-argument constructor for a Structure then you will end
up in the following error:

“Structs cannot contain explicit parameterless constructors”

• You can define and use events and delegates in Class as well as Structure.

• Both Class and Structure inherit from the class Object. Even though Structure is a value type, .NET provides unified type system based on which all value types and reference types have their base class as Object.

| How do you prevent a class from overriding in .NET? | How are classes related to objects in .NET Application | How are Delegates different from Events in .NET? | How are system exceptions different from application exceptions in .NET? | How are Value Types different from Reference Types in .NET? | How can a finalize method be suppressed in .NET? | How can you call Stored Procedure in ADO.NET? | How can you force Dispose method to be called automatically in .NET? | How do you call a Base Class Constructor from Derived Class Constructor in .NET? | How do you connect your VB.NET application to SQL Server? | How do you implement Cloning in .NET? | How do you implement Façade Design Pattern in .NET? | How do you implement MVC Pattern in ASP.NET? | How do you install .NET Assembly in GAC? | How is shadowing different from overriding in .NET? | How to prevent a particular .NET DLL from being decompiled? | Illustrate Delay Signing Process of an Assembly in .NET? | What are Reference Types in .NET? | What are the advantages of C#? | What are the advantages of VB.NET? | What are the differences between Namespace and Assembly in .NET? | What are the similar features between class and structure in .NET? | What are Value Types in .NET? | What do you mean by mixed mode authentication in .NET? | What do you mean by Satellite Assembly in .NET? | What do you mean by shadowing in .NET? | What is CTS in .NET? | What is ILDASM in .NET? | What is Managed Code in .NET? | What is Manifest in .NET? | What is MSIL in .NET Framework? | What is the importance of finalize method in .NET? | What is the need for Visitor Pattern in C#? | What is the purpose of bindingRedirect tag in web.config file of .NET? | What is the purpose of CodeDom in .NET? | What is the purpose of dispose method in .NET? | What is the purpose of Ngen.exe in .NET? | What is the purpose of Strong Name in COM Components of .NET? | What is the purpose of virtual keyword in .NET? | What Object Oriented Principles can be incorporated in .NET Application? |


“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2024 - All Rights Reserved.