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.
Related
This question already has answers here:
new DateTime() vs default(DateTime)
(4 answers)
Closed 2 years ago.
if(new DateTime() == default(DateTime))
{
//blah blah...
}
Code 'new DateTime()' will create new instance and take some memory for itself.
I want to know is Code 'default(DateTime)' also create new instance and take some memory or just compare and then end
As stated here, it creates a new default value. So its same memory allocation as it happens for value type and reference type. First one on stack second on heap
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/default#default-literal
For value types, it sets to zero, e.g. default int is 0. For reference types default is null.
See the docs for details.
Since DateTime is a value type, it will be allocated on the stack, rather than the heap. It will only take up memory inside the current stack frame, or if it is a member of a class.
This question already has answers here:
What's the difference between passing by reference vs. passing by value?
(18 answers)
Closed 5 years ago.
I've been trying to understand passing by reference and passing by value and trying to understand it lead me to trying to understand reference data types. In all the explanations I've seen online they define a reference data type to be referencing data as opposed to containing data, and i'm wandering what is the difference?
What i think so far is that the variable doesn't contain data itself but it is referencing a method, but i don't feel like that is a satisfactory understanding. Hopefully someone could clear this up for me.
When you pass the reference of an variable, you are actually sending the memory address of that variable, whoever getting the address will be able to change your variable's value. but if you are passing by value, you are sending a copy of your variable, so even if the receiver changes the value of that variable, you will still get the original value of your variable.
This question already has answers here:
What's the difference between the 'ref' and 'out' keywords?
(28 answers)
Why ref and out in C#?
(7 answers)
Closed 9 years ago.
As per this post, the reason there is a distinction between ref and out is because it is costly to copy the value of the variable when using ref.
Why is there a need to marshall in the first place? Doesn't C# just pass the pointer under the hood? In that case, there would be no need to copy values.
Because the semantics of the two are completely different.
An out parameter is used to indicate that it will be used to return (output) a value, nothing more.
A ref parameter on the other hand indicates that an existing object (variable) should be passed to the method by reference. In the context of C#, an object passed by reference (not to be confused by reference types) is often a hint that the method will (and should) modify that object. It shouldn't be used "just because." It is generally used only for value types since it is the only way to get reference semantics for them.
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.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Why isn't String.Empty a constant?
I wanted to use string.Empty in the Attributes of a few of my properties, but have since seen that it is not actually a Const but a static member.
Is there any reason Microsoft would do this?
I would say it's always a pretty bad idea using const in referenced assemblies.
The reason being the fact that the C# compiler treats constants as values and not as references, as I've said in this answer.
With this I mean that the C# compiler will replace all instances of the constant in you code and replace the "variable" with the value.
This means that even if you update the assembly GlobalConstants.dll and copy it to one of the applications you have, you will need to recompile that application. Not doing so, will cause the application to use the old constant values.
To overcome this problem, you can simply use public static readonly instead of public const as the readonly modifier differs from the const in that it is treated by the C# compiler as a reference in code rather than a value.
I think that the reason is: string is reference type, not value type. and it is faster to compare two references (when you use static member) than two instances of strings (when you use const)