Results 1 to 5 of 5

Thread: Still confused about structs

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2002
    Posts
    15

    Question Still confused about structs

    I just started programming in C# about three days ago. I'm still fairly confused on when I should be using structs vs. classes.

    Thus far, I know that structs are value types and performance can be increased by utilizing them.

    I also know that if I want a parameterless constructor for any reason classes are the only way to go. As well - classes can have destructors while structs can not.

    Half the time I just can't decide on whether to use the struct or class keyword. Should I simply use structs if possible? And if so...does this mean many programmers overutilize classes?
    Nightmare

  2. #2
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861
    Rule of thumb...classes can be inherited (derived) from...Structs can't. You create an instance of a class which gets stored in memory (a reference type, created on the heap). A struct is a value type and created on the stack (which allows for faster retrieval of the information).

    say you wanted to create something about employees in you company. you could do it with a class.

    Base Class Employee

    Then you could create another class called SalariedEmployee which inherits the base functionality of the EmployeeClass.
    Then you could create another class called HourlyEmployee.
    See where I am going with this???

    or you could just create a structure to do this for you.
    Code:
    public struct Employee
    {
       string firstName;
       string lastName;
       boolean salaried;
       etc...
    
    }
    Classes are generally used to implement a piece of business logic, rather than to support data related primitive like objects.
    Last edited by Memnoch1207; Dec 3rd, 2003 at 10:05 AM.
    Being educated does not make you intelligent.

    Need a weekend getaway??? Come Visit

  3. #3
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Simply Struct is variables grouped under one name .

  4. #4
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682
    Nightm@re, if you have any VB6 experience, a struct is more or less the same as a user defined Type (UDT).

  5. #5

    Thread Starter
    New Member
    Join Date
    Nov 2002
    Posts
    15

    Thanx

    Thanx for all the responses .

    After reading all your replys and programming a bit more the differences between the two have become much more apparent.
    Nightmare

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width