• Home
  • About
  • Contact
  • ado.net
  • angular
  • c#.net
  • design patterns
  • linq
  • mvc
  • .net core
    • .Net Core MVC
    • Blazor Tutorials
  • sql
  • web api
  • dotnet
    • SOLID Principles
    • Entity Framework
    • C#.NET Programs and Algorithms
  • Others
    • C# Interview Questions
    • SQL Server Questions
    • ASP.NET Questions
    • MVC Questions
    • Web API Questions
    • .Net Core Questions
    • Data Structures and Algorithms

Tuesday, July 25, 2017

OOPS – Difference Between Class and Struct

 Abhishek Tomer     July 25, 2017     .Net, C#     No comments   

Hi friends! Today we are going to discuss a very quite interesting and important topic of Object Oriented Programming(OOPs) i.e.“Classes and Structures“.

So firstly we'll start from the definition of each and then we'll go through the similarities and the differences between them.
Q.What is a Class?
A.Classes are blueprints(building block) describing attributes(properties) and behavior(methods) that you use to create objects while programming.
Q.What is a Structure?
A.Similarly as of a Class,Structures are also another user defined data type that are available in C# that allows to combine data items of different kinds.
So, these were the definitions of Classes and Structures.
Now, Let's find out the Similarities and Differences between Classes and Structure.

Here are some differences between a Class and a Struct.
Class Struct
Classes are reference types Structs are value types.
Can support inheritance Cannot support inheritance
The reference can be null Cannot have a null reference (unless Nullable is used)
Have memory overhead per new instance Do not have a memory overhead per new instance - unless 'boxed'
Class can have default constructor. Struct can only have parametrized constructors.
Class can both implement interfaces and inherit interfaces. Struct can’t inherit or implement interfaces.
Here are some Similarities between a Class and Struct.
Class Struct
Can Implement Interfaces. Can Implement Interfaces.
Members are Private by default. Members are Private by default.
Can have Main() Can have Main()
Can have Parameterized constructor Can have Parameterized constructor(but with the condition that “constructor must initialize all the member variables.”)
Can have methods. Can have methods.
We will start by simple interface implementation. Let’s create an interface with one method.

public interface MyInterface
    {
        int foo();
    }
Now let’s first have a look at a simple class.
public class MyClass
    {
        int a;
    }
This class compiles fine. Now let’s implement the interface defined above
public class MyClass: MyInterface
{
int a;
}
Now when you compile this class you will get a compile time error stating that MyInterface.foo() is not implemented in MyClass. If you don’t want to implement foo in MyClass then you must declare MyClass as abstract class. Let’s now look at 2 more scenarios of interface implementation
public class MyClass : MyInterface
    {
        int a;
        public void foo()
        {
            return a;
        }
    }
This time the class compiles fine.

Summary:
So, Guys this is all about the similarities and differences between a class and a struct.
I Hope in this post I have covered all the points about Classes and Structs which will be helpful to you.

Please share this post with your friends and colleagues.

For any queries please post a comment below.

Happy Coding 😉
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Newer Post Older Post

0 comments:

Post a Comment

If you like this website, please share with your friends on Facebook, Twitter, LinkedIn.

Join us on Telegram

Loved Our Blog Posts? Subscribe To Get Updates Directly To Your Inbox

Like us on Facebook

Popular Posts

  • What is Dependency Injection(DI)
    Hi friends! Today we are going to learn about Dependency Injection and in our last session we have come across Static classes and where it s...
  • ASP.NET State Management
    State management is a technique or way to maintain / store the state of an Asp.Net controls, web page information, object/data, and user in ...
  • What is Abstract Class and When we should use Abstract Class
    Hi friends! In our previous sessions we have seen  Difference Between Class and Struct . And in our last session  we learnt Usability of Sec...
  • Static Files Middleware in ASP.NET Core
    In this article, I am going to discuss how to serve static files using Static Files Middleware in ASP.NET Core Application. Please read ou...
  • HTTP Client Message Handler in Web API
    In this article, I am going to discuss HTTP Client Message Handler in Web API with real-time examples. As we already discussed in HTTP Mes...
  • ASP.NET Web API Basic Authentication
    In this article, I am going to discuss how to implement the ASP.NET Web API Basic Authentication step by step with an example. Please read...
  • Kestrel Web Server in ASP.NET Core
    In this article, I am going to discuss the Kestrel Web Server in ASP.NET Core Application. Please read our previous article before proceed...

Blog Archive

Contact Form

Name

Email *

Message *

Tags

.Net .Net Core .Net Core MVC Algorithm Angular Anonymous Types Asp.Net Asp.Net MVC Blazor C# Data Structure Database Design Patterns Entity Framework Entity Framework Core Filters Interview Question Management Studio Programming Programs SQL Server SSMS Web API

Copyright © C# Techtics | All Right Reserved.

Protected by Copyscape