I have a C# class and all of its members are of type object
.
What are the risks and warnings? Will it take more resources?
What is the difference between object and Object in C#?
I have a C# class and all of its members are of type object
.
What are the risks and warnings? Will it take more resources?
What is the difference between object and Object in C#?
There is no difference between object
and Object
. object
is just a built-in keyword of C# that stands for Object
(or rather System.Object
), just as int
stands for System.Int32
.
Why are all your properties of object
type? It doesn't take more resources (except for type checking and type casting), but there is definitely a risk, because you don't know which types you are working with at compile time. You would need to check types and cast all the time. If you would use more specific types, the compiler would take care of checking that for you. Are you sure there are no more appropriate types for your properties, such as int
, string
or something else?
A member that is of the type object is just a reference to something else. That means that you have a bunch of reference that you have to cast to their actual type in order to use them for anything, which also means that you have to keep track of what their actual types are. If possible you should change the type to the actual types that you want to store.
An object reference in itself doesn't use more or less resources than any other reference type, but as you have to cast it everytime you use it, it will take more code to work with the data.
The keyword object
in C# is an alias for the System.Object
class in the framework.
The only problem is that you will have to cast the objects to the real types in order to manipulate them. If you know that you will use a string or an int, it is better to use string or int instead of object. Also, this will make the code easy to read in the future. Object is too generic and does not tell you anything.