Classes and Structs in C#
page 1 of 1
Published: 22 Sep 2011
Unedited - Community Contributed
Abstract
There are two main concrete ways of encapsulating data and behavior in the common type system of the .NET Framework: classes and structs. Each one offers benefits over the other, and in this article I am going to briefly introduce these two concepts and discuss how to use each and when to use each based on their differences.
by Eric Fleming
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 8403/ 18

Introduction

There are two main concrete ways of encapsulating data and behavior in the common type system of the .NET Framework: classes and structs. Each one offers benefits over the other, and in this article I am going to briefly introduce these two concepts and discuss how to use each and when to use each based on their differences.

Each of these defines a schema for an object that we can create encapsulating the data and behavior that we want into these objects that we can create at runtime. We are able to create these objects and use them in our code freely at runtime. We can sometimes change the values, use them, pass them around. In object oriented programming, classes and structs are an integral piece of the programming paradigm.

Classes

Classes are the most commonly used of these two forms of encapsulation. The main difference between the two is that classes are a reference type, which means that by default they're used by reference instead of by value.

Structs

Structs are the lesser used of these two forms of encapsulation. The primary distinction of structs is that a struct is a value type, which means that it is stored on the stack instead of the heap and it is passed by value instead of by reference by default.

Structs are behave similarly to the other value types that we see when writing .NET code. These include: integers, doubles, bools, and other value type objects.

Structs don't require constructors, but if there is one, they require that there are parameters in the constructor.

Structs do not require the use of the new keyword in creation, but if you use this syntax you need to initialize the members manually.

Structs do not have access to inheritance.

When to Use Each

Classes are generally used for complex data with lots of behavior as well as for objects which may become large. One major reason for this is that classes never need to be copied when passing them around this means that the same object gets reused and we don't have to be concerned with duplicating such a large object.

Structs are generally used in situations where we need light, quick objects which are not often changed and are used in context. They are less often used when there will be a large amount of behavior associated with the objects.

One great benefit of structs is the avoidance of allocating to the heap and thus the overhead of garbage collection is avoided, but be mindful of doing this with large objects as passing them to methods will require copying them since they are value types.

Passing data to and from programs running in c or c++ is one instance where structs are used instead of classes. These are the objects able to do this work for us.

Conclusion

Structs and classes are both great ways of managing code and each have their benefits and drawbacks. Try each one and see how well they work in your code. Experiment with structs as value types and classes as reference types. Try passing them into methods and then change them in the method and notice the difference. Structs don't have side effects from this.



User Comments

Title: more sample code needed   
Name: softprov
Date: 2012-05-06 2:35:44 AM
Comment:
need more sample code. please add more samples.thanxs.
Title: add some more concepts   
Name: angalena
Date: 2012-04-12 6:38:37 AM
Comment:
add some more concepts with small examples
Title: Useful   
Name: Mohit
Date: 2011-11-08 11:35:56 PM
Comment:
Nice article, however I would add a few declaration examples.
Thank you!
Title: Nice article   
Name: dotNetFollower
Date: 2011-09-23 10:34:46 PM
Comment:
Nice article, however I would add a few declaration examples.
Thank you!






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-10-15 11:48:58 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search