Difference between structure and class

csharp , structure , class India
  • 11 years ago

    What is the difference between structure and class in C#?

  • 11 years ago

    Hi

    I am a new programmer but try to be some help as I can with you to suppoert. Well let me try to start always the simplest.

    Class:

    In C# language, a class is defined by using the class keyword. For example, public class MovieList{....}. A class is a reference type and its object is created on the heap. A class can be inherited from a class or struct. An instance field or a member variable can be initialized in a class. A class cannot be created without using the new keyword. For example, MovieList movieList= new MovieList (id,name,tracknr¨, ......);

    Structure

    In C# language, a structure is defined by using the struct keyword. For example, public struct MovieList{....} A structure does not support inheritance and cannot be inherited from any class or struct. A structure is more efficient when used in arrays. An instance field or a member variable cannot be initialized in a struct. A struct can be created without using the new keyword. For example, *MovieList *

    Hopefully this might be some help and meets your requirements. Hilsen = best regards, Ahmed Yassin

  • 11 years ago

    The difference, in non-technical English:

    A Class has many uses in the object oriented environment of programming. One of its major advantages is that it can act as a blueprint for a new program you are about to write. For example, if your are building houses -- each house is different, one storey, two storey, three bedroom, five bedroom, etc, BUT there are some common elements in ALL houses -- the roof, floor, windows, door, etc. These common elements can go in a "House.class" file. So, when you are writing a "OneStoreyHouse.class" application for example, you can make an instance of the House.class in the OneStoreyHouse.class and be able to use all the pre-defined elements in that class to build your new class.

    A Structure on the other hand deals with the data-type issue. if you are writing a job application program, you will need different types of data -- string (name), int (age), float (salary), char (m/f), etc. Normally, you would have to create an individual variable for each data type, so you would end pp with 4 variables. A structure can combine these 4 variables into 1 data-type, so you would only have to create 1 variable as this new data-type. eg: public struct ReqData { public string Name; public int Age; public char Gender; public float Salary; } appdata;

    So, by defining appdata User, the single variable called User will now contain 4 different data-types.

    Hopes this helps create a clear picture!

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Measuring programming progress by lines of code is like measuring aircraft building progress by weight.” - Bill Gates