This is a discussion on Difference between a struct and a class in C# within the C# Programming forums, part of the Software Development category; I want to know the difference between a struct and a class in C#.Can anybody explain?...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| hi, 1. Classes are reference types and structs are value types. Since classes are reference type, a class variable can be assigned null.But we cannot assign null to a struct variable, since structs are value type. 2. When you instantiate a class, it will be allocated on the heap. but struct gets created on the stack. 3. You will always be dealing with reference to a class as object. But you will not be dealing with references to an instance of a struct 4. When passing a class(object) to a method, it is passed by reference. When passing a struct to a method, it's passed by value instead of as a reference. 5. You cannot have instance Field initializers in structs.But classes can have initializers. 6. Classes can have explicit parameterless constructors. But structs cannot have explicit parameterless constructors. 7. Classes must be instantiated using the new operator. But structs can be instantiated without using the new operator. 8.Classes support inheritance.But there is no inheritance for structs. 9.Since struct does not support inheritance, access modifier of a member of a struct cannot be protected or protected internal. 10. A class is permitted to declare a destructor.But a struct is not permitted to declare a destructor. 11. classes are used for complex and large set data. structs are simple to use. structs are useful whenever you need a type that will be used often and is mostly just a piece of data. Regards Manivannan.s |
| |||
| hi, Quote:
*Structures use stack allocation; classes use heap allocation. *All structure members are Public by default; class variables and constants are Private by default, while other class members are Public by default. This behavior for class members provides compatibility with the Visual Basic 6.0 system of defaults. *A structure must have at least one nonshared variable or event member; a class can be completely empty. *Structure members cannot be declared as Protected; class members can. *A structure procedure can handle events only if it is a Shared Sub procedure, and only by means of the AddHandler statement; any class procedure can handle events, using either the Handles keyword or the AddHandler statement. *Structure variable declarations cannot specify initializers, the New keyword, or initial sizes for arrays; class variable declarations can. *Structures implicitly inherit from the ValueType class and cannot inherit from any other type; classes can inherit from any class or classes other than ValueType. *Structures are not inheritable; classes are. *Structures are never terminated, so the common language runtime (CLR) never calls the Finalize method on any structure; classes are terminated by the garbage collector, which calls Finalize on a class when it detects there are no active references remaining. *A structure does not require a constructor; a class does. *Structures can have nonshared constructors only if they take parameters; classes can have them with or without parameters. *Every structure has an implicit public constructor without parameters. This constructor initializes all the structure's data members to their default values. You cannot redefine this behavior. |
| |||
| The list of similarities between classes and structs is as follows. Longstructs can implement interfaces and can have the same kinds of members as classes. Structs differ from classes in several important ways; however, structs are value types rather than reference types, and inheritance is not supported for structs. Struct values are stored on the stack or in-line. Careful programmers can sometimes enhance performance through judicious use of structs. For example, the use of a struct rather than a class for a Point can make a large difference in the number of memory allocations performed at runtime. The program below creates and initializes an array of 100 points. With Point implemented as a class, 101 separate objects are instantiated-one for the array and one each for the 100 elements. |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Difference between abstract class and interface | srikumar_l | C# Programming | 0 | 12-17-2007 03:18 AM |
| difference between abstract class and interface | vijayanand | Java Programming | 2 | 11-27-2007 09:19 PM |
| Differences between a C++ struct and C++ class | vijayanand | C and C++ Programming | 0 | 09-21-2007 03:35 AM |
| List out all the similarities/difference between an Abstract class and Interface? | H2o | ASP and ASP.NET Programming | 1 | 07-24-2007 03:16 AM |
| What’s the difference between an interface and an abstract class? | Sabari | Java Programming | 1 | 07-17-2007 05:00 AM |