|
This question already has an answer here:
I want to assign a variable to an initial value of null, and assign its value in the next
if-else block, but the compiler is giving an error,
Implicitly-typed local variables must be initialized.
How could I achieve this?
Peter Mortensen
14.2k1919 gold badges8888 silver badges115115 bronze badges
Nikhil ChavanNikhil Chavan
99511 gold badge1212 silver badges3131 bronze badges
![]() C# Initialize List With Null Valuesmarked as duplicate by nawfal, vcsjones, iandotkelly, Selman Genç, Jim LewisJan 17 '14 at 5:08
This question has been asked before and already has an answer. Materi ukt merpati putih. If those answers do not fully address your question, please ask a new question.
2 Answersvar variables still have a type - and the compiler error message says this type must be established during the declaration.
The specific request (assigning an initial null value) can be done, but I don't recommend it. It doesn't provide an advantage here (as the type must still be specified) and it could be viewed as making the code less readable:
Which is still 'type inferred' and equivalent to:
The compiler will not accept
var x = null because it doesn't associate the null with any type - not even Object. Using the above approach, var x = (Object)null would 'work' although it is of questionable usefulness.
Generally, when I can't use
var's type inference correctly then
The second approach can be done by moving code into methods or functions.
user2246674user2246674
The
var keyword in C#'s main benefit is to enhance readability, not functionality. Technically, the var keywords allows for some other unlocks (e.g. use of anonymous objects), but that seems to be outside the scope of this question. Every variable declared with the var keyword has a type. For instance, you'll find that the following code outputs 'String'.
Furthermore, the code above is equivalent to:
The
var keyword is simply C#'s way of saying 'I can figure out the type for myString from the context, so don't worry about specifying the type.'
var myVariable = (MyType)null or MyType myVariable = null should work because you are giving the C# compiler context to figure out what type myVariable should will be.
For more information:
Community♦
Steven WexlerSteven Wexler
9,70555 gold badges3838 silver badges6464 bronze badges
Not the answer you're looking for? Browse other questions tagged c#var or ask your own question.
How can I initialize an empty list as a private member in a C++ class? I don't want to prompt the user to pass in an empty list, but rather just create one when I make a new instance of the class.
header.h
MyClass.cpp
This doesn't work as written, what am I missing here?
LBaelishLBaelish
33211 gold badge55 silver badges1616 bronze badges
2 Answers
You don't need to do anything. The default constructor, like the name suggests, is called by default. So this is already correct:
![]()
If you want to still have BarryBarry
myList in the member initializer list, you can do that by simply providing empty parens:
194k2222 gold badges359359 silver badges643643 bronze badges
If you want to optionally hand in a user-defined value, you can use the initializer list of your constructor. Additionally, default the list to an empty content.
user1357959
Not the answer you're looking for? Browse other questions tagged c++classinitialization or ask your own question.
I am creating a new C# List ( Peter Mortensen
List<double>). Is there a way, other than to do a loop over the list, to initialize all the starting values to 0?
14.2k1919 gold badges8888 silver badges115115 bronze badges
JasCavJasCav
26.3k1818 gold badges9898 silver badges156156 bronze badges
6 Answers
In addition to the functional solutions provided (using the static methods on the
Enumerable class), you can pass an array of doubles in the constructor.
This works because the default value of an Michael MeadowsMichael Meadows
double is already 0, and probably performs slightly better.
23.8k44 gold badges4242 silver badges5959 bronze badges
You can use the initializer:
So you could be using this:
Otherwise, using the default constructor, the List will be empty.
Peter Mortensen
14.2k1919 gold badges8888 silver badges115115 bronze badges
Jhonny D. Cano -Leftware-Jhonny D. Cano -Leftware-
13.4k1111 gold badges7373 silver badges9999 bronze badges
Peter Mortensen
14.2k1919 gold badges8888 silver badges115115 bronze badges
SLaksSLaks
707k142142 gold badges16801680 silver badges17941794 bronze badges
One possibility is to use
Enumerable.Range:
Another is:
jasonjason
200k2929 gold badges372372 silver badges495495 bronze badges
A bit late, but maybe still of interest:Using LINQ, try
var initializedList = new double[10].ToList()
..hopefully avoiding copying the list (that's up to LINQ now).
This should be a comment to Michael Meadows' answer, but I'm lacking reputation.
MickyD
11.5k66 gold badges3434 silver badges5656 bronze badges
DanielDaniel
For more complex types:
from here: David Hayden's Blog
ColinColin
Not the answer you're looking for? Browse other questions tagged c#.netlistcollections or ask your own question.
Simple syntactic c# question is this.
Shadow of the tomb raider hidden city challenges 2016. Sep 14, 2018 - Dunkin' Bones (Hidden City). If there's one thing there's definitely a lot of in Shadow of the Tomb Raider, it's skeletons. So the thought of finding.
Given this code:
It can be reduced to:
But can I get it shorter still, since they're all being initialised to an empty list?
Thank you all!
Adrian HandAdrian Hand
11322 gold badges55 silver badges1717 bronze badges
4 Answers
I can recommend to use
var keyword if these variables are not class fields, because types are know from usage. It really makes no sense to flat declaration of three variables.
Yes, you can do things, like declaring multiple local variables in one line and then initializing them in one line. But please, avoid declaring multiple variables in one line - it makes code much readable.
Ilya IvanovIlya Ivanov
19.9k44 gold badges5050 silver badges7878 bronze badges
Purely as a point of trivia/code golf: Best file recovery software for android.
But it would be ridiculous to use this in place of the much clearer constructs available. It might have value if the initialization was more complex, and the code was properly named and spaced.
Tim MedoraTim Medora
47.9k99 gold badges9999 silver badges137137 bronze badges
You can't shorten anymore.
You could use some kind of 'empty list factory':
Honestly, you probably don't want to shorten your code. Just write something readable and maintainable:
ken2kken2k
41.5k77 gold badges8484 silver badges147147 bronze badges
This is not a great practice, but if you're initializing string lists a lot within a single .cs file, you can add an alias for the class with the file's other
using statements:
And then the code to declare and initialize them would be:
C Sharp Initialize ListJLRisheJLRishe
78.6k1111 gold badges8383 silver badges122122 bronze badges
C# Empty ListNot the answer you're looking for? Browse other questions tagged c# or ask your own question.Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |


RSS Feed