user536158
user536158

Reputation:

Choosing the right data type vs casting

I am trying to learn to write good C# code and i want your advice.

If i have for example an unsigned number value that is smaller than 255 so this value fits in the byte type. But this value will not be used as byte but as an Int to set the combobox index so it will needs casting.

My question is how much do I have to worry about data types when writing good C# code? Do I need to declare this value as an Int to avoid casting?

Thank you for your time.

Upvotes: 3

Views: 196

Answers (7)

user536158
user536158

Reputation:

Thank you guys for your answers. I couldn't accept one answer because they complement each other so I decided to group them.

  1. The CPU is designed to work efficiently with 32-bit values (linked question).
  2. There is no need to do premature optimization.
  3. It is not efficient to always cast data types.
  4. Declaring a variable of certain type and always casting it reduces code readability.

Upvotes: 0

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236328

Design of your data types should not depend on way how they displayed by controls (or saved to data storage). If I see property of type byte then it tells me what range of values my data type expects. E.g. Color class has self-descriptive properties R,G,B of type byte.

Also do not do premature optimizations - if it become performance problem (I have doubts that casting from byte to int could have big influence on system performance) then you can optimize problem code later.

BTW casting from byte to int is implicit, so you don't need to do it manually:

comboBox.SelectedIndex = myObj.ByteProperty;

Upvotes: 0

Guy Lescalier
Guy Lescalier

Reputation: 11

With the amount of memory currently available on our computers, memory is not an issue anymore when it's time to choose between byte or int.

I'd add that an int is much easier to deal with.

Upvotes: 0

Nathan
Nathan

Reputation: 6216

Data typing is fundamental to good coding - it lets compilers do a lot of checking for you and therefore helps eliminate/highlight bugs.

In your example, you could use the ushort to store your number, but unless you're writing a very memory sensitive application, you will be better off using an int as many library calls will need to convert the parameter back to an integer anyway. (this will make your code less readable due to constant type casting - and possibly less efficient too)

Upvotes: 0

AgentFire
AgentFire

Reputation: 9800

The answer is yes.

When you need to use not very big integer value you should avoid using/casting to any other types but int unless you have the needs in it. Like limited bandwidth where you are sending this data.

Upvotes: 0

Oded
Oded

Reputation: 499352

If you need it to be an int, use an int.

Data types are important, but so is the issue of code readability - having to cast all over the place reduces readability.

Upvotes: 2

Tudor
Tudor

Reputation: 62469

Casting from byte to int is safe since int is wider than byte, so no data loss is possible.

However, if you are defining a variable as byte but actually casting it to int all the time, it's better to just make it int, since you would only be confusing yourself and other readers of your code.

Upvotes: 2

Related Questions