0

What is the difference Method1 and Method2 in declaration?

Method1

private readonly CategoryBusiness _categoryBusiness = new CategoryBusiness();

Method2

private readonly CategoryBusiness _categoryBusiness;

public CategoryController() : this(new CategoryBusiness())
{

}

public CategoryController(CategoryBusiness categoryBusiness)
{
    _categoryBusiness = categoryBusiness;
}

Update: My exact question is

Is initializing a readonly field at its declaration the same as initializing it in the constructor with the same value?

Theodoros Chatzigiannakis
  • 28,773
  • 8
  • 68
  • 104
  • The first method declares a new object, where the second method passes the parameter to variable and declares it then with the parameter. – jAC May 07 '13 at 09:40
  • 2
    Is this a riddle? A quiz? Where can we send answers? What can we win? Please provide a meaningful question that later visitors benefit from. Now you're just asking us to copy the C# manual. – CodeCaster May 07 '13 at 09:40
  • 1
    @CodeCaster, Can you provide the link for the answer to my question in C# manual. I use to always to google it first before raising question here.... Thanks... –  May 07 '13 at 09:43
  • 2
    You can look up the manual for each part on which you have a question. Now your question just asks what the _"difference in declaration"_ is, which is easy: [readonly (C# Reference)](http://msdn.microsoft.com/en-us/library/acdd6hb7.aspx): _"When a field declaration includes a readonly modifier, **assignments to the fields introduced by the declaration can only occur as part of the declaration or in a constructor in the same class**"_. So that's your answer. – CodeCaster May 07 '13 at 09:46
  • 1
    (cont) In Method1 you assign a variable at the declaration, in Method 2 you do it from the constructor. I'm just curious what you will do with this answer and how it will help you or others, that's all. – CodeCaster May 07 '13 at 09:48
  • @CodeCaster, Your and these answers surely helped me. So i found no difference in both the method and so from now i can use the first method and reduce the code lines. Even small things helps better... Thanks... –  May 07 '13 at 09:51
  • So then perhaps your question was _"Is initializing a readonly field at its declaration the same as initializing it in the constructor with the same value"_? Then yes. If you'll look at the IL the compiler generates there, you'll see it'll silently move the initialization into the constructor. – CodeCaster May 07 '13 at 09:55
  • @CodeCaster, Thanks again, i updated my question. Can you please let me know in detail about your last comment. "If you'll look at the IL the compiler generates there, you'll see it'll silently move the initialization into the constructor.". It will be better if all these comments can be an answer. –  May 07 '13 at 09:59
  • 1
    Just see this question: http://stackoverflow.com/questions/2761393/static-readonly-field-initializer-vs-static-constructor-initialization – CodeCaster May 07 '13 at 10:07

4 Answers4

5

Its same, readonly field can be assigned a value with declaration or in a constructor.

readonly (C# Reference)

When a field declaration includes a readonly modifier, assignments to the fields introduced by the declaration can only occur as part of the declaration or in a constructor in the same class.

Initializing it through the constructor has the added benefit of assigning the field a value during object initialization. (as LukeHennerley's comment)

The readonly keyword is different from the const keyword. A const field can only be initialized at the declaration of the field. A readonly field can be initialized either at the declaration or in a constructor. Therefore, readonly fields can have different values depending on the constructor used.

Habib
  • 219,104
  • 29
  • 407
  • 436
  • +1 Thanks, So will there be any difference without readonly? –  May 07 '13 at 09:40
  • +1 :) Although the assignment is the same you have a bit more control in the constructor in that you can modify the passed in object before assignment. – LukeHennerley May 07 '13 at 09:41
  • @SSS, without `readonly`, you can leave it as it is, not initialized. But with `readonly` it must be initialized either during declaration or in constructor – Habib May 07 '13 at 09:41
0

Obviously, Method 2 allows to specify a CategoryBusiness object in the constructor, while Method 1 doesn't.

ken2k
  • 48,145
  • 10
  • 116
  • 176
0

They're identical in terms of initializing something to a fixed value. However, the latter is more flexible in that it allows someone to specify another instance, which could among other things be useful for subclasses.

BambooleanLogic
  • 7,530
  • 3
  • 30
  • 56
0

The first assigns a value to _categoryBusiness before the constructor is called (so you can use is right in the constructor). The second one assigns a value to _categoryBusiness in the constructor so you can use it only after this assignment. The advantage about the second one is that you can change this readonly value from the calling code. The advantage of the first is that it is really readonly.