Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# C# Objects Methods Method Overloading

Angad Chadha
Angad Chadha
1,467 Points

Why do we need to initialize a primitive variable?

Is it necessary to initialize a variable? Does it affect the code? Or is it just a convention used by programmers?

2 Answers

Steven Parker
Steven Parker
231,248 Points

You can always declare a variable without initializing it. There are some cases where this is desirable (such as when it is about to be used as an out parameter).

But when a variable is being created to hold a value which is already known (or can be obtained) it's just good practice to initialize it when it is created.