Exclusively for you!! Trial version of Google Apps, Apply Coupon-CNHPGX6T6QRWGV!!

Consider Structs

OOPS-class-object @Image courtesy of digitalart / FreeDigitalPhotos.net

Consider Structs

Welcome to CodeSpread!

While taking birth in programming world, we all had our own experiences of learning; begin working on a new language or OOPS concepts or experimenting with a new tool or compilers. One of such experiences is already available here.

Let’s share one more personal experience and an embarrassing one. J

I was just caught up with c# and begin traversing the beautiful journey of programming. I was very overwhelmed with my OOPS knowledge and coding skills. I was making applications, enjoying cups of coffees and playing ping-pong with my team.

One day, for a requirement, I was asked to create a program and I created it using lot of meaningless lines of codes. Then during review, I came to know that it could have been done by structs. It was quite embarrassing that I could not recollect structs, just because we do not use it in our daily programming. It was my early phase so was not crucified. Happy go lucky!!

I realized that my instant reaction to almost any requirement was class, oops and objects as though it is limited water in the pond. But programming is an ocean. So just to prove that I know structs, I will share structs syntax and knowledge here.

What is Structs?

A struct type is a value type that can contain constructors, constants, fields, methods, properties, indexers, operators, events, and nested types. [msdn]

struct Example


public int x, y;

public Example (int x, int y) {

this.x = x;

this.y = y;



Few points about Structs:

  • Structs are value types.
  • Struct values are stored on the stack.
  • Inheritance is not supported.
  • Structs can implement interfaces.
  • Structs can improve memory allocations in case of lightweight objects.
  • Structs cannot have a default (parameterless) constructor.

Let’s see an example to see structs’ value type nature. For the same struct declared above, we will use it in the below program.

Class StructExample


public void AssignStruct(Example instance)


instance.x = 1;

instance.y = 2;



public static void Main()


Example example = new Example ();

example.x = 3;

example.y = 4;







The result will be



Here, struct “Example” being a value type creates a copy and was passed to the AssignStruct method. So value of the struct field remains same in the output and will not have effect of AssignStruct Method.

Few lines of code and lightweight object is what the requirement might be, so always consider structs before jumping to class based programming.

Author: swatiS

Share This Post On


  1. nice superb explaination

    Post a Reply
  2. thanks for the tutorial
    I am having a problem.
    The feature “Create SQL server database” isn’t available , I can’t select it.
    Can you help, please ?

    Post a Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

More from CodeSpread:

  • Why Multiple Inheritance is discouraged?Why Multiple Inheritance is discouraged?Hi Guys, This is Sathish Again,Today we will discuss why most of the higher languages eliminates Multiple Inheritance? Most of you guys already know that multiple inheritance is not available for...
  • Concepts: S O L I DConcepts: S O L I DSOLID - Object oriented programming principles. May be its easy to forget these principles  but for creating a reusable code or maintainable code, people will always have to come back and check ...
  • Few lines about Static code analysisFew lines about Static code analysisStatic code analysis We divide the term into two parts: Static+code analysis. Static in programming can be referred to as non-running/non-dynamic and code analysis is a process where the code is...
  • Time Out: Not a Technical postTime Out: Not a Technical postTaking my time-out from technical world for once and sharing a regular joke in the form of  this comic with you. There is no harm in showing funny side also sometimes. wat say!!
  • Interview Series: Polymorphism Question- Answer UpdatedInterview Series: Polymorphism Question- Answer UpdatedWe are sharing our third question of the series in the form of comic, Please help our kid in the comic by sharing your answers through comments or Facebook  or Google plus or twitter[@CodeSpreadBlo...
  • How to view Intermediate Code in C# using Ildasm.ExeHow to view Intermediate Code in C# using Ildasm.ExeHi guys, this is Sathish again, today let’s discuss how to view the intermediate code in c#. We know that CLR (Common Language Runtime) is used to convert the MSIL (Microsoft Intermediate Language...
  • Agile Encounters UnpredictabilityAgile Encounters UnpredictabilityWhat is Agile? Wikipedia says “Agile software development is a group of software development methods based on iterative and incremental development, where requirements and solutions evolve through...
  • Styles of Source Code Comments in C#, SQL, XML, HTML, CSS, JavaScriptStyles of Source Code Comments in C#, SQL, XML, HTML, CSS, JavaScriptDevelopers take months to write beautiful pieces of code and reviewer gets only few hours or I say, minutes to provide suggestions and improvisations. Then it becomes a hard deal for a developer to...