Click to See Complete Forum and Search --> : class...struct...interface...Messed up??
ChessMaster
January 28th, 2008, 12:58 AM
I am a rookie here coming from c++ background. I have been doing a little reading. Dont feel very comfortable with the definition class, struct, & interface. Personally I feel this is messed up quite a bit.
Do let me know your views on the same.
What was need of such an segregation? Coud they have not combined alll the required functionality in one - a class and let the programmer decide how to go about it. Dosent that make more sense rather than...
****************************************************************************************
1. Class is reference type
2. Struct is value type
and
3. new is used to allocate objects on the heap
In C#, the new keyword can be used as an operator or as a modifier.
new operator (http://msdn2.microsoft.com/en-us/library/51y09td4%28VS.71%29.aspx#vclrfnew_newoperator) Used to create objects on the heap and invoke constructors.
BUT--->
Although new is an operator used to allocate objects on the heap
SomeStruct obj = new SomeStruct();
This new creates an object on the stack since its "value type" :sick:
****************************************************************************************
1. Does it not make sense to have class defined as having x, y, z characterstics, and let the programmer decide, when he wants it on the stack and when on the heap.
2. Now considering (1) where is the need of a struct?
3. Interface.
My initial impression of interface is that it could cause some serious design problems making u to fall back on a class at times. If i am right about this, again comes back to we cud have done with just a class with the functionality combined in one.
Kindly let me know ur views on the same
ChessMaster
January 28th, 2008, 01:12 AM
One other point...
If we have a class - SomeClass and a struct - SomeStruct
and objClass and objStruct are their instances respectively.
SomeClass objNewClass = objClass; (1)
SomeStruct objNewStruct = objStruct; (2)
(1) is just a reference while (2) is totally a new object.
When u have a project consisting 1000's of lines of code. Everytime u come across such code u need to see its declaration to find out if one has created a copy or an object. Or fall back heavily on good naming conventions.
boudino
January 28th, 2008, 02:36 AM
To your question about "new" - C# is designed this way. Maybe be it is because of simplicity (yes, as one can see on you, not everybody consider it to be simplier). Keep in mind, that classes is major concept in C#. Structs are minor and should be used carefully and only if you realy know what you are doing. Their purpose is to provide better implementation for objects, which can be represented directly in the terms of machine code, typicaly by a number. If so (and it is case of numbers, timestamps etc...), it is better to make copies of it, because every copy is identical and immutable ("1" is "1", it has no state, it cannot be changed in any way).
To you questions about interfaces: interfaces allow you to grow from "programming with classes" to "object oriented programming." They allow you to focus on contract and loose coupling, which highly increase flexibility and maintainability of the application.
MadHatter
January 28th, 2008, 02:49 AM
forget the man behind the curtain. just use new and move on with it.
structs are value types, and as such you can obtain its address / size / and is very much needed in situations such as interop w/ native code.
classes are managed entities. you can't get their address / size and can't extract its info from memory like you can value types.
interfaces exist because of a lack of multiple inheritance, or maybe as a result of the problems that face multiple inheritance. they are a type of pure virtual abstract class.
riscutiavlad
January 28th, 2008, 02:56 AM
Structures are value types and classes are reference types and there are quite a few differences. For instance, a structure always has a default constructor which sets all its members to default values; also, you can pass structures as arguments to native methods. Being created on the stack, there is less overhead in accessing structs rather than classes.
Structures should be used when the primary functionallity is to store a set of elements (Rectangle for example, or Point, which have the primary purpose of storing some int values).
Interfaces represent a contract that a class agrees to fulfil when implementing an interface. C# doesn't support multiple inheritance, but a class can implement multiple interfaces. This a very useful construct when you want to make sure that a class implements certain methods. For example, you could use:
interface IAccount
{
void CreateAccount(string userName);
}
class LocalAccount : LocalOperations, IAccount
{
public void CreateAccount(string userName)
{
base.DoLocalStuff();
}
}
class WebAccount : WebOperations, IAccount
{
public void CreateAccount(string userName)
{
base.DoWebStuff();
}
}
class AccountManager
{
public void CreateAdminAccount(IAccount account)
{
account.CreateAccount("admin");
}
}
Eventhough WebAccount derives from a different class than LocalAccount and they both do completely different things, they can create accounts and guarantee that by implementing the IAccount interface. Now you can pass to the account manager either a LocalAccount object or a WebAccount object and an admin account will be created.
Mutant_Fruit
January 28th, 2008, 03:14 AM
An object contains 16 bytes of overhead (if i remember correctly). That means if you want to create an object containing just an int and then store 1,000,000 of them in memory, you'll get these stats:
Struct: 4 * 1,000,000 bytes used
Object: 20 * 1,000,000 bytes used.
Whilest that's not a big reason to choose one over the other, it's important to bear in mind. 99% of the time you want a class, even if you are storing a few million in memory.
Structs are stored on the stack, as you point out, which has one big advantage:
while(true)
mystruct s = new MyStruct(); // This will not allocate more memory.
The above snippet will just reinitialise the memory space of the struct over and over. This can be a huge advantage. If that were a class, you would be forcing garbage collections and allocating memory for millions of new objects a second.
Once again, this is not a huge win for struct, only certain circumstances benefit from this.
Structs are useful for interop with native code though.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.