C class structure LEARNOVITA

Understanding Structures in C# | Learn in 1 Day FREE Tutorial

Last updated on 18th Aug 2022, Blog, Tutorials

About author

Jagan Mohan (Senior C# Developer )

Jagan Mohan is a Senior Senior C# Developer and has extensive knowledge in the following areas of WPF, XML, REST, Agile, V model, C#, SQL server, Web API, and ASP.NET.

(5.0) | 17589 Ratings 2075

Introduction Structures in C#:

In C#The struct is the value type data sort that addresses data structures. It will contain an outlined builder, static builder, constants, fields, strategies, properties, indexers, directors, occasions, and settled kinds. A structure is often used to carry very little data esteems that don’t would be requested, for instance, coordinate focuses, key-esteem sets, and complicated data structure.

Structure Declaration:

A construction is declared utilizing struct slogan. The default modifier is inward for the struct and its people. The attendant model proclaims a construction Coordinate for the chart.


struct Coordinate

Defining Structure:

To characterize a construction, you must utilize the struct clarification. The struct articulation characterizes another data sort, with over one half for your program. For instance, here is however you’ll be able to pronounce the Book structure .


struct Books ;

What square measure structures in C#?

A construction in C# may be a composite data sort comprising many elements of various kinds. … the look in C# will contain fields, techniques, constants, constructors, properties, indexers, directors, and astonishingly alternative construction sorts.

Does C# have structs?

C# structs

In C#The struct is the price kind data kind that addresses data structures. It will contain an outlined builder, static builder, constants, fields, techniques, properties, indexers, directors, occasions, and settled types.

Would we tend to be ready to create the occasion of struct in C#?

From struct (C# Reference) on MSDN: once you create a struct object utilizing the new administrator, it gets created and therefore the correct builder is named. In distinction to categories, structs are often started up while not utilizing the new administrator.

What area unit structures utilized for in C#?

Structures are cheap for putting away basic, low price articles that may very often be controlled frequently, as an example, int or books. That is an on the spot result of however they administer memory. Structs area unit esteem sorts tho’ categories area unit reference sorts. categories area unit assigned separate memory on the oversaw store whereas structs involve the assessment stack.Sit back and relax on the off probability that that didn’t appear OK! What it implies in useful terms is that you simply don’t create another incidence of struct utilizing the new watchword. it’s utilized sort of a price kind, as an example, int.

This saves time whereas recovering/adjusting your factors, as they’re gotten to foursquare while not expecting to bring employing a reference. This incorporates a few ramifications as we’ll see later. This time improvement isn’t progressing to considerably impact giant numbers of your normal application use cases. Yet, will demonstrate imperative for programs wherever speed is basic, as an example, in laptop games.

Understanding Structures in C#:

A style in C# is simply a composite data kind comprising many elements of various sorts. A C# structure may be price kind and therefore the occasions or objects of the construction area unit created during a stack. the look in C# will contain fields, techniques, constants, constructors, properties, indexers, directors, and amazingly different construction sorts.

Structure Declaration and Object Creation:

The slogan struct is often used to announce a construction. the general form of a construction statement in C# is as per the subsequent.

    1. 1. struct
    2. 2. people

Where the modifiers are often non-public, public, interior, or public. The struct is the necessary slogan. The objects of a struct are created by involving the new administrator as follows. MyStruct ms = new MyStruct(); The singular people from a struct is obtained by utilizing the spot (.) administrator as showing at a lower place.

Recall that not the least bit like categories, the struct item will likewise be created while not utilizing the new administrator. MyStruct ms. Yet, for this example, all fields of the struct can keep unassigned and also the item can’t be used till all of the fields are instated.

Structs and Fields:

A struct in C# will contain fields. These fields are declared as a non-public, public, interior. call back that within a struct, we will pronounce a field. we have a tendency to not introduce a field within a struct. Anyway, we will utilize the builder to instate the look fields.

Struct and Methods:

A C# struct will likewise contain ways. The way is either static or non-static. Be that because it could, static techniques will get to simply alternative static people and that they can’t conjure by utilizing associate objects of the development. they’ll summon solely by utilizing the struct name.

Structs and Constructors:

A C# struct will pronounce builder, however, they ought to settle for boundaries. A default (constructor with next to no boundaries) area unit given all of the time to introduce the struct fields to their default esteems. The outlined constructors within a struct will likewise be over-burden.

Structs and Properties in C#:

The property is declared within a struct as displayed beneath.

    1. 1. //C#: Property
    2. 2. //Author: rajeshvs@msn.com
    3. 3. exploitation System;
    4. 4. category MyStruct
    5. 5. personal int x;
    6. 6. public int X
    7. 7. set
    8. 8. }
    9. 9. }
    10. 10. category MyClient
    11. 11. public static void Main() ten
    12. 12. }
    13. 13. }

Structs and Indexers:

The indexers will likewise be utilized with a C# struct. A model is displayed at a lower place.

    1. 1. //Author: rajeshvs@msn.com
    2. 2. exploitation System;
    3. 3. exploitation System.Collections;
    4. 4. struct MyStruct come data[index];
    5. 5. }
    6. 6. set
    7. 7. }
    8. 8. }
    9. 9. category MyClient ,,,,”, ms[0], ms[1], ms[2], ms[3], ms[4]);
    10. 10. }
    11. 11. }

Structs and Inheritance:

There is no heritage for structs as there is for categories. A struct can’t acquire from another struct or category and it can’t be the bottom category for a category. In any case, recall that in C# various sorts are unit squarely or by implication non inheritable from the terribly base category object and therefore the development too. Since structs don’t uphold heritage, we have a tendency to can’t utilize the watchwords virtual, abrogate, new, unique, and so on with a struct strategy. C# struct sorts area unit seldom distinctive and area unit actually mounted 100 percent of the time. The theoretical or mounted modifiers aren’t allowed in an exceedingly structured presentation.

Since heritage isn’t upheld for structs, the announced convenience of a struct half can’t be safeguarded or safeguarded inner. Since all struct sorts are actually non inheritable from object category, it’s possible to get rid of the techniques for the item category within a struct by utilizing the watchword supervene upon. recall that this can be AN exceptional case in C# structs.

Structs and Interfaces:

Very much like categories, a C# struct will likewise execute from a degree of interaction. for example

    1. 1. //Author:rajeshvs@msn.com
    2. 2. exploitation System;
    3. 3. interface IInterface
    4. 4. struct Complex: IInterface
    5. 5. }
    6. 6. category MyClient complicated c1 = new Complex();
    7. 7. c1.Method();
    8. 8. }
    9. 9. }//This is barely Associate in Nursing example script. Glue your real code (javascript or HTML) here.
    10. 10. if (‘this_is’ ==/an_example/) else volt-ampere a = b ? (c nada d) : e[f];
    11. 11. }

Characterize a construction if:

    1. 1. The cases of the type are very little, like crude kinds (whole variety, twofold, bool).
    2. 2. you wish to esteem linguistics rather than reference linguistics.
    3. 3. it’s changeless.
    4. 4. it’s an incident size shorter than sixteen bytes.
    5. 5. Doesn’t ought to be boxed currently and once more.
    6. 6. No demand for polymorphism.
    7. 7. wish to stay aloof from load allotment and also the connected trash assortment upward.

Remember, that with regards to execution, categories might in any case finally end up faster by and by. as passing structs as contentions are a lot more pricey than categories.

Features of C# Structures:

You’ve made good use of a fundamental structure called Books. Structures in C# are terribly not constant as that in standard C or C++. The C# structures have the attendant elements:

  • Designs will have ways, fields, indexers, properties, administrator techniques, and occasions.
  • Designs will have characterized constructors, but not destructors. however, you can’t characterize a default builder for a style. The default builder is consequently characterized and can’t be modified.
  • Not in any respect like categories, structures can’t acquire completely different styles or categories.
  • Structures can’t be utilized as a base for various styles or categories.
  • A style will perform a minimum of one purpose of interaction.
  • Structure people can’t be determined as dynamic, virtual, or secured.
  • At the purpose once you build a struct object utilizing the New administrator, it gets created and also the fitting builder is termed. In distinction to categories, structs are often started up while not utilizing the New administrator.
  • If the New administrator isn’t utilized, the fields keep unassigned and also the article can’t be utilized till all of the fields are instated.

Class versus Structure:

Classes and Structures have the attendant elementary contrasts:

  • classes are reference varieties and structs are esteem varieties.
  • structures don’t uphold the bequest.
  • structures can’t have a default builder.

Instantiation of a construction type:

In C#, you ought to instate an associated proclaimed variable before it tends to be utilized. Since a construction sort variable can’t be invalid (except if it’s a variable of a nullable value sort), you ought to kick off a case of the scrutiny sort. There are multiple ways of doing that. Ordinarily, you launch a style sort by career, a correct builder with the new administrator. every style sort has no but one builder. That associate understood a parameterless builder that creates the default value of the sort. you’ll be able to likewise utilize a default esteem articulation to make the default value of a kind.

Assuming that every one example field of a construction sort area unit is obtainable, you’ll be able to likewise startup it while not the new administrator. All things thought of, you ought to instate all example fields before the principal utilization of the occasion. The attendant model tells the most effective thanks to do that:


  • C#Copy
  • public static category StructWithoutNew
  • public static void Main()
  • , )”);//yield: (3, 4)
  • }
  • }

C# Structure Characteristics:

Coming up next at the numerous attributes of constructions within the c# programming language.

  • In c#structures are esteem sorts, and people are characterized by utilizing struct watchwords.
  • During the development announcement, the fields can’t be instituted except if characterized as const or static.
  • Structures in c# will incorporate fields, properties, part works, directors, constructors, occasions, indexers, constants, and astonishingly alternative style sorts.
  • Structures will exclude the default (constructor while not boundaries) or destructor, however, it’ll allow a North American country to announce constructors with boundaries.
  • A construction can’t be acquired from another style or category.
  • In c#, the development will do interfaces.
  • A construction is often launched with or while not utilizing another watchword.

C# distinction among Structure and Class:

Coming up next at the distinction among styles and categories within the c# programming language.

  • In c#categories are the reference sorts, and styles at the price types.
  • Classes will contain default constructors or destructors, however styles can contain simply constructors that have boundaries.
  • We can execute gift utilizing categories, however structures won’t uphold the gift.
  • In distinction to categories, structs are often launched with or while not utilizing another administrator.

Purpose for Struct in C#:

Struct is AN exemplified part. it’s a value info sort. all of the people from struct should be started. it’s an easy consumer characterized info sort that may be utilized as an option to category. The struct watchword is employed for creating a construction.

Structures have practices and properties. they will foursquare contain the upsides of the article or example from the stack. They support access modifiers, constructors, indexers, techniques, directors, fields, and properties.

Implement the Struct in C#:

The struct is often characterized by utilizing the struct proclamation. They add a minimum of one individual utilized within the program. the construction for characterizing a struct is as displayed beneath:

The understudy structure contains 3 people as student id, name and course. If you’re returning from Associate in Nursing alternate programming language, it’s not tough to sink into Associate in Nursing examples of utilizing categories throughout your application.In any case, structures in Swift square measure deeply vast – they have been given huge significance throughout the Swift commonplace Library itself. You moreover ought to keep them in mind for your applications. The concomitant arrangement of benefits and drawbacks ought to assist you with deciding on an alternative in such a fashion.


  • You can’t proclaim a parameterless creator. every style sort as of currently provides Associate in Nursing tacit parameterless creator that makes the default price of the type.
  • A creator of a style ought to instate all incidence fields of the sort.
  • A style sort can’t acquire from different categories or construction sorts and it can’t be the inspiration of a category. Be that because it could, a style sort will execute interfaces.
  • You can’t pronounce a finalizer within a construction sort.
  • You can’t introduce an Associate in Nursing example field or property at its revelation. with, you’ll be able to instate a static or const field or a static property at its affirmation.


    1. 1. Structs square measure unnoticeably faster at runtime than categories, thanks to advancements done by the compiler.
    2. 2. you’ll be able to authorize full unchanging nature. presumptuous you announce a struct occasion as let, you’ll be able to not amend its properties. Though, with categories, unchanging nature is simply regarding the reference command by the steady – you’ll in any case have the choice to alter its properties.
    3. 3. Since structs square measure esteem sorts, every case could be a special duplicate of knowledge. On these lines, whereas utilizing structs in an exceedingly multi-string climate, every string might have its duplicate of the knowledge, Associate in Nursing odds square measure low of 1 string dynamic data out beneath you in an alternate string. If your case of the struct is changeable, or save for presumptuous that you simply mix reference sorts in you struct (see beneath), the chances of this incident square measure higher – but presumptuous that you’re utilizing a category, even the insufficient security you’d get is lost.


    1. 1. Structs don’t uphold the gift. Contingent upon your perspective, this could not be one thing awful, however, this can be unquestionably one thing to recollect whereas designing your models.
    2. 2. They can’t go with a category convention (duh), like NSObjectProtocol. This seems to be a plain comment, however you will never grasp once you need to start agreeing to a convention – and a lot of Foundation conventions (for instance) are characterized as category conventions.
    3. 3. You lose character (===) examination. Character examination is efficacious in cases like occasion taking care of once you gotta check that article radiated the occasion. With categories, you can do, for example, if (source === occasion).
    4. 4. square measure cool once utilized as pure price types, i.e. a solitary struct composed of different price types – structs or Enums. Yet, whereas you bring reference linguistics into matters, things will get untidy. The issue is, whereas a struct is replicated by esteem on task, any property that’s a reference kind is duplicated by – you bought it – reference. no matter whether or not it’s set apart as let, if the reference kind is modified away, the struct is to boot compact.


For learning style of C# for amateurs, it’s expected to possess elementary computer data, essential data on Object-Oriented programming approach, and elementary data on C programing language ideas to understand C# rudiments with none drawback.


These upgrades to the Structures in C# language area unit meant for the execution of basic calculations wherever limiting memory parts could be a central purpose in accomplishing the essential exhibition. you may see that you simply don’t often involve these components within the code you compose. all the same, these enhancements are taken on for the duration of. NET. As a lot of genus Apis utilize these highlights, you’ll see the exhibition of your applications move on.

Are you looking training with Right Jobs?

Contact Us

Popular Courses