What's the difference between Initialization and Assignment in C# [duplicate] - c#

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
C# variable initializations vs assignment
Just like in the title, could someone please explain what is the difference between Initialization and Assignment in C#? I'm preparing for a test and I wanted to know what's the best way to answer this type of question. Thanks
Cheers,
n1te

When you initialize a variable you're declaring it into existence.
PlasticCup mySippyCup = new PlasticCup();
When you assign, you're just saying "this water" goes into "this cup". The cup already exists.
mySippyCup = new PlasticCup();

Initialization is assigning value while declaring the variable - int a = 1
Assignment is just assigning value to a variable - a = 1
By this definition, some say all initializations are assignments, but all assignments are not initializations.

Related

The meaning of the var I entered is the same. Why do I have different results when running? [duplicate]

This question already has answers here:
How to initialize var?
(12 answers)
Closed 8 months ago.
var b1;
b1 = true;
Console.WriteLine(b1);
If my code runs it throws an error hermit-typed variable must be initialized
var b1 = true;
Console.WriteLine(b1);
And I do it like this and it compiles normally
I can't understand
This is correct var required an assignment. Now in your first example your assignment is in another line so the compiler can't deduct the type of the variable b1 from that.
Instead in your second example the declaration and assignment occur in the same line so the compiler know wich type assign to the b1 variable.

Object initialization vs property assignment [duplicate]

This question already has answers here:
Setting properties via object initialization or not : Any difference ?
(5 answers)
Is there any benefit of using an Object Initializer?
(5 answers)
Closed 3 years ago.
This morning, I had a discussion with a co-worker regarding the following two example of initializing the code.
Example 1:
var employee = new Employee();
employee.FirstName = GetFirstName(); // this may get values from db
Example 2:
var employee = new Employee()
{
FirstName = GetFirstName()
}
My co-worker's argument was that example 2 is more efficient and if that object has any errors during initialization, it is automatically removed from heap. I am thinking that the compiled version of the code would look similar to example 1.
My question is that is my co-worker correct in his argument? When should you use property assignment instead of object initialization? Are there any best practices around this? MS doesn't have anything ...

Use of unassigned local variable 'x' [duplicate]

This question already has answers here:
Why do local variables require initialization, but fields do not?
(4 answers)
Closed 6 years ago.
This code:
int a;
if(a==1){ do something}
or
Image img;
if(img!=null){do something}
These codes generates the error:
Use of unassigned local variable 'variable-name'
I know what exactly this error says and how to resolve it. but what I wonder is that
Shouldn't a variable that is declared and not assigned a value, have a value? like null for the Image
2.Why wouldn't it allow to even compare the variable? it can simply say that it doesn't match.
Shouldn't a variable that is declared and not assigned a value, have a value? like null for the `Image'
No. That's just not the way the language is defined. Local variables do not have default values in C# (or most other languages, but by no means all).
2.Why wouldn't it allow to even compare the variable? it can simply say that it doesn't match.
Because you're trying to read the value of a variable that you've never assigned a value to, which is a bug (in C#) by definition. Thus, it's calling your attention to that bug.

Modifying static variables from other scripts in Unity [duplicate]

This question already has answers here:
What is the use of static variable in C#? When to use it? Why can't I declare the static variable inside method?
(12 answers)
Closed 7 years ago.
Does anybody know how to modify a static variable from a different script in Unity?
Please, provide more information about how are you trying to do this...
If you need to change between mono behaviours, for example, you will need to get current instance of behaviour you like to change in a way like
myGameObjectInstance.GetComponent<MyBehaviourWithStatic>().myStatic = value;
but note that user2320445 answer is not wrong, it depends on your context. Be more specific on your question and we could provide better and precise answers

Is Nullification Advisable? [duplicate]

This question already has answers here:
Setting Objects to Null/Nothing after use in .NET
(15 answers)
Closed 8 years ago.
I don't mean to start a[nother] civil war over this issue, but should nullification be enforced or not?
While trying to fix a bug (that was fixed elsewise, as can be seen here), I added code like this in a couple of places to immediately and explicitly set some objects to null:
List<String> XMLFiles = CCRUtils.GetXMLFiles(fileType, #"\");
foreach (string fullXMLFilePath in XMLFiles)
{
RESTfulMethods.SendXMLFile(fullXMLFilePath, uri, 500);
}
XMLFiles = null;
Now that I know that did not solve the problem I was having, should I just leave it, or remove this type of code?
should nullification be enforced or not?
In general, it should not. There are very few circumstances where there is a reason to set a variable to null. As soon as the scope in which the variable is declared ends, the reference to the object will no longer be held.
Setting a variable to null after use is (almost) always just a misunderstanding of how the GC works, and serves no purpose.

Categories