Lost Password?

Go Back   CodeCall Programming Forum > Software Development > Tutorials > CSharp Tutorials

CSharp Tutorials Tutorials for C#

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-04-2008, 03:37 PM
Jordan's Avatar   
Jordan Jordan is offline
Administrator
 
Join Date: Nov 2005
Location: Hendersonville, NC
Age: 26
Posts: 5,846
Last Blog:
Performance or Maintai...
Rep Power: 20
Jordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud of
Send a message via ICQ to Jordan Send a message via AIM to Jordan Send a message via MSN to Jordan
Default Understanding the differences between a class and struct in C# :

Understanding the differences between a class and struct in C# :
If you are not an experienced programmer, you might be confused by the classes and the structs. Assuming that you might be already a little confused by the differences between function and classes, we will try to explain in detail the structs here.


Struct: Doesn't hold a value;
Class: Holds a value

Garbage collection:
As you probably know one of the major difference between C# and C++ is how the developer manages memory. In C# the memory is easily allocated by the garbage collector. Now, however this is partly true, because when we work with structs the memory is not directly allocated unless you use reference types and these types refer to a real value that invokes the garbage collector.

The This KEYWORD, WHEN working with structs:
In the classes the "this" keyword always defines a value, it cannot be used with keywords ref and out. Consider the following example, it will produce a compiler error. So you better don't try to remember it...:

Code:
class demonclass
{
    //...
    public void Method(demonclass that)
    {
        RefParameter(ref this); // compile error
        OutParameter(out this); // compile error
        this = that;            // compile error
    }
    //...
}
The right implementation when we use struct:

Code:
struct theright
{
    //...
    public void Reassign(theright that)
    {
        RefParameter(ref this); // compiles ok
        OutParameter(out this); // compiles ok
        this = that;            // compiles ok
    }
    //...
}
A struct can be used even in cases with readonly values, consider the following example:



Code:
struct theright
{
    public theright(int value)
    {
        Field = value;
    }
     
    public void Reassign(theright that)
    {
        RefParameter(ref this); // compiles ok
        OutParameter(out this); // compiles ok
        this = that;            // compiles ok
    }
    
    public readonly int Field;
}

class justaclass
{
    static void Main()
    {
        theright s = new theright(112);
        Console.WriteLine(s.Field); // the output is 112
        s.Reassign(new theright(24));
        Console.WriteLine(s.Field); // the output is 112
    }
}
Another major difference is that a class might design its objects as private and hence they might not be accesed further in the program. On the other hand the objects are always public.

The struct cannot be null:
Code:
using System;

class Nullclass
{

    static void Eg(int? x)
    {

        if (x != null)
        {
            Console.WriteLine("This is OK");// ok
        }

    }
    public static void Main()
    {

    }
}
If everything went right you should see nothing on your screen...but only the empty prompt when you start it without debugging:




Can the struct use destructors?
NO. Because they don't need to, since they don't use the memory allocation, garbage collectors and so on.


How a class handles inheritance and how a struct handles inheritance:
The structs are sealed, the classes are not.
The class can't be abstract. The class could be abstract(meaning that the class can be used as a parent class).
The structs cannot use the base() method. The classes can.
The structs cannot extend other structs, and the classes can extend classes.
Protected members can't be declared within structs. The classes can declare such members(objects).
a struct can't declare abstract function members, an abstract class can.
The struct can't declare abstract, virtual and sealed functions. The classes can do that...
__________________
CodeCall Blog | CodeCall Wiki | Shareware Site | Linux Forum | Write a Blog
Don't hesitate to ask any questions that you have! Check out our ASCII Calculator!

Last edited by Jordan; 04-04-2008 at 03:42 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 04-18-2008, 05:23 AM
morfec morfec is offline
Newbie
 
Join Date: Apr 2008
Posts: 7
Rep Power: 0
morfec is on a distinguished road
Default Re: Understanding the differences between a class and struct in C# :

thanks thanks thanks
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-19-2008, 08:33 AM
Xav's Avatar   
Xav Xav is offline
Code Warrior
 
Join Date: Mar 2008
Location: London, England
Posts: 4,899
Last Blog:
Web slideshow in JavaS...
Rep Power: 42
Xav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to behold
Send a message via MSN to Xav
Default Re: Understanding the differences between a class and struct in C# :

I tend to use structs, as they are generally easier to make. However, for bigger projects, classes are the way to go.
__________________
[TRUTH] TcM is the best moderator ever! [/TRUTH]
"Valid XHTML is like sex - everybody claims to have the same goal, but everybody has their own tricks and results vary wildly."
Web Site | Beta Site
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-20-2008, 01:21 AM
gaylo565's Avatar   
gaylo565 gaylo565 is offline
Programmer
 
Join Date: May 2007
Location: flagstaff, az
Posts: 144
Last Blog:
String Manipulation wi...
Rep Power: 6
gaylo565 has a spectacular aura aboutgaylo565 has a spectacular aura about
Default Re: Understanding the differences between a class and struct in C# :

I find structs useful for structuring data in useful ways when simple arrays wont do the trick, but for most things a class is more versatile. No destructors is a nice touch for structs though...Writing less code is always a plus if you have limited needs.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 04-20-2008, 09:32 AM
Xav's Avatar   
Xav Xav is offline
Code Warrior
 
Join Date: Mar 2008
Location: London, England
Posts: 4,899
Last Blog:
Web slideshow in JavaS...
Rep Power: 42
Xav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to behold
Send a message via MSN to Xav
Default Re: Understanding the differences between a class and struct in C# :

There's not necessarily a problem with more code, although it can become difficult to manage. Which is partially why classes were invented.
__________________
[TRUTH] TcM is the best moderator ever! [/TRUTH]
"Valid XHTML is like sex - everybody claims to have the same goal, but everybody has their own tricks and results vary wildly."
Web Site | Beta Site
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply

Tags
class, struct



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

BB 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
OPENFILENAME struct (win32) - problems shariath C and C++ 1 02-14-2008 10:07 PM


All times are GMT -5. The time now is 09:47 PM.

Contest Stats

John ........ 223.00000
dargueta ........ 168.00000
Xav ........ 164.00000
gaylo565 ........ 18.00000
WingedPanther ........ 15.00000
|pH| ........ 15.00000
Johnnyboy ........ 3.00000
navghost ........ 1.00000

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 65%

Ads