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: } Executing
displayMsg from sampleClass 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: 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 { Output of this code will be: Calling No-Argument
Constructor of sampleClass If you try
to explicitly define a no-argument constructor for a Structure then you
will end Structs cannot contain explicit parameterless constructors You
can define and use events and delegates in Class as well as Structure.
|