1

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

Toon Krijthe
  • 52,876
  • 38
  • 145
  • 202
Rauf Zeynalov
  • 110
  • 3
  • 14
  • There's [no difference between `object` and `Object`](http://stackoverflow.com/questions/1017282/c-difference-between-system-object-and-object). Apart from that, avoid `object` wherever you can and use strong types (f.e. an `int`,`string`,`Foo`,...). – Tim Schmelter Oct 09 '12 at 06:50
  • They even compile to exactly the same thing. They're just an alias. Same goes for things like `System.Double` and `double`, `System.Int32` and `int`, etc... – Matthew Kennedy Oct 09 '12 at 06:56

3 Answers3

1

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?

Botz3000
  • 39,020
  • 8
  • 103
  • 127
0

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.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • I am working with Devexpress and EditValue properties of the components are object type.They can be null too,I am taking the values and sending them to stored procedure as parameters,for that reason I want to use object type. – Rauf Zeynalov Oct 09 '12 at 07:00
0

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.

Alexandru Dicu
  • 1,151
  • 1
  • 16
  • 24