IT Community - Software Programming, Web Development and Technical Support

Difference between a struct and a class in C#

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?...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > C# Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 09-01-2007, 08:26 AM
Sathish Kumar Sathish Kumar is offline
D-Web Analyst
 
Join Date: Feb 2007
Posts: 304
Sathish Kumar is on a distinguished road
Exclamation Difference between a struct and a class in C#

I want to know the difference between a struct and a class in C#.Can anybody explain?
__________________
Sathish Kumar.R
Knowledge is meant to SHARE
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 09-03-2007, 07:40 AM
smani smani is offline
D-Web Programmer
 
Join Date: Aug 2007
Posts: 56
smani is on a distinguished road
Default Re: Difference between a struct and a class in C#

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
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 09-03-2007, 07:46 AM
kingmaker kingmaker is offline
D-Web Genius
 
Join Date: Jun 2007
Posts: 882
kingmaker is on a distinguished road
Send a message via Yahoo to kingmaker
Default Re: Difference between a struct and a class in C#

hi,

Quote:
Originally Posted by Sathish Kumar View Post
I want to know the difference between a struct and a class in C#.Can anybody explain?
*Structures are value types; classes are reference types.
*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 KINGMAKER
Makes Every Thing Possible

Stuffs (My Blog)
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 09-05-2007, 08:11 AM
Mramesh Mramesh is offline
D-Web Sr.Programmer
 
Join Date: Sep 2007
Location: Chennai
Posts: 106
Mramesh is on a distinguished road
Send a message via MSN to Mramesh
Thumbs up Re: Difference between a struct and a class in C#

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.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


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


All times are GMT -7. The time now is 11:56 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0