0

I was wandering which way of doing below code is better :

a)

    byte[] tmp = BitConverter.GetBytes(Number)

b) 
    byte[] tmp = new byte[sizeof(Number)]
    tmp = BitConverter.GetBytes(Number)

Is it necessary to use dynamic memory allocation ?

2
  • What is Number? Does sizeof(Number) compiles? Commented Jul 22, 2014 at 8:42
  • Number was an example of some numeric object Commented Jul 22, 2014 at 8:44

3 Answers 3

4

Definitely a).

b) creates two arrays, the first of which is completely unnecessary and is thrown away right after being initialized.

Sign up to request clarification or add additional context in comments.

Comments

2

Actually second one is redundant, because GetBytes returns a new array so you are throwing away the first array you created...

Comments

1

The first is better for two reasons:

  1. It is more readable
  2. In sample b the first allocation is thrown away as tmp is re-assigned to the return value of GetBytes

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.