|
Thanks Pete,
1.
For String.Empty, as you said, it represents a string initialized, but length is zero. Initialized means constructed (constructor called)?
2.
All variables initialized to String.Empty are pointing to the same memory address or not?
e.g.
String a = String.Empty;
String b = String.Empty;
a and b pointing to the same memory area?
regards,
George
|
|
|
|
|
George_George wrote: For String.Empty, as you said, it represents a string initialized, but length is zero. Initialized means constructed (constructor called)?
Yes.
string empty1 = "";
string empty2 = string.Empty;
string empty3 = new string(''); etc.
George_George wrote: All variables initialized to String.Empty are pointing to the same memory address or not?
Yes. String is a reference type. Therefore when you assign a string to another variable you are just assigning the reference.
|
|
|
|
|
Hi Colin,
This line can not compile -- "string empty3 = new string('')". What do you want to show?
regards,
George
|
|
|
|
|
George_George wrote: For String.Empty, as you said, it represents a string initialized, but length is zero. Initialized means constructed (constructor called)?
No, as it is static, there will only be 1 instance, and you simply refer to that single instance every time. That specific instance will only be created once, when the string type initializer runs.
|
|
|
|
|
Thanks leppie,
You mean all references to String.Empty are pointing to the same instance -- an internal instance returned by String class which represents zero length string?
regards,
George
|
|
|
|
|
2)
If All the variables initialized to <pre>String.Empty</pre> Then They will point to <b>different</b> memory address and all those memory addresses will contain same value "Zero Lenght String".
It is not possible that you create two different reference type variables and they have same memory reference, unless you assign one variable to another.
also remeber that
string is immutable. so every time you change the value it will be allocated and initialize with the new value again.
so if you want to do frequent string processing then use StringBuilder class.
String vs StringBuilder[^]
|
|
|
|
|
Rutvik Dave wrote: If All the variables initialized to
String.Empty Then They will point to different memory address
Incorrect. Here is what reflector has to say on the subject:
public static readonly string Empty;
That means there is only one representation ever in memory. It is not copied, it is referenced. That means they will ALL point to the same memory address.
Rutvik Dave wrote: It is not possible that you create two different reference type variables and they have same memory reference, unless you assign one variable to another.
That is correct, but it does not apply to the current situation as you are not creating different objects. You are referencing an existing object.
Rutvik Dave wrote: remeber that string is immutable. so every time you change the value it will be allocated and initialize with the new value again
Also correct, but does not apply in this question.
|
|
|
|
|
Colin Angus Mackay wrote: Rutvik Dave wrote:
It is not possible that you create two different reference type variables and they have same memory reference, unless you assign one variable to another.
That is correct, but it does not apply to the current situation as you are not creating different objects. You are referencing an existing object.
In case of strings it is possible to create two instances of a string object that contains the same constant string. Then the two reference types will point to the same place in memory (string interning). For example:
string str1 = "hello world";
string str2 = "hello world";
if (str1 == str2) Console.WriteLine("same reference");
|
|
|
|
|
Erm, == is overriden for string, so it tests for value equality, not reference equality. But in this case, constant strings will be 'auto' interned by the compiler.
|
|
|
|
|
leppie wrote: Erm, == is overriden for string
Thanks for pointing that out, you're right[^]
|
|
|
|
|
It is great to always read more.
regards,
George
|
|
|
|
|
|
Thanks Zoltan,
1.
I have tested and get the same result from you. I think for string, since == opeartor is overridden, it compares for content of strings, correct?
2.
And for general Object type comparsion, and for the types derived from Object and is not overridden equality comparison operator, I think the memory address other than the content of instances are compared.
regards,
George
|
|
|
|
|
Yes George correct, it is stated clearly in Msdn at the link I posted:
Two string values are considered equal when one of the following is true:
* Both values are null.
* Both values are non-null references to string instances that have identical lengths and identical characters in each character position.
Sometimes it's just better to look up the info.
|
|
|
|
|
Always great to learn from you, Zoltan!
regards,
George
|
|
|
|
|
Colin Angus Mackay wrote: Incorrect. Here is what reflector has to say on the subject:
public static readonly string Empty;
That means there is only one representation ever in memory. It is not copied, it is referenced. That means they will ALL point to the same memory address.
Yes I am agree with you.
string s = ""; will create an object in the memory
string s = String.Empty; will NOT create any object, its just a reference.
I also found this on MSDN :
Framework 3.5
((object)"") == String.Empty :::: False
("") == String.Empty :::: True
Framework 1.1 & 2.0
((object)"") == String.Empty :::: True
("") == String.Empty :::: True
Thanks for pointing out the mistake. from now onwards i will use String.Empty to save memory overhead.
|
|
|
|
|
Great Rutvik!
I think all references to String.Empty are pointing to the same memory address, where the static member of Empty locates, right?
regards,
George
|
|
|
|
|
Thanks for your attention, Colin!
1.
What do you mean "It is not copied, it is referenced"? Could you show some pseude code please?
2.
"create two different reference type variables and they have same memory reference" -- sorry I do not agree, here is an example, you create a base type variable (reference type) and a derived type variable (reference type), they are of different types but pointing to the same memory address. Please feel free to correct me if I am wrong.
regards,
George
|
|
|
|
|
George_George wrote: What do you mean "It is not copied, it is referenced"? Could you show some pseude code please?
You have two types of objects. Value types and Reference types. Value types are always copied when you pass them around. Reference types are always passed by reference.
You can think of references in code like the references in academic papers. When you read an academic paper you may come across a reference that reads like this [GoF1994]. When you turn to the end of the paper you will see listed all the references. The reference [GoF1994] may be used several times throughout the paper, but it always points to the same item in the list at the end.
If it was copied all the time you wouldn't have a reference and you would have stuff like this copied throughout the paper [Gang of Four, Effective Design Patterns, 1994, Addison Wesley]. By copying the source paper everywhere the academic paper just got much larger. However, a reference can be used to quickly note what academic paper is being used as the source. The reference points to the full detail somewhere else rather than have the full detail repeated constantly.
|
|
|
|
|
Thanks Colin!
Your reply is clear.
regards,
George
|
|
|
|
|
George_George wrote: "create two different reference type variables and they have same memory reference"
This comment was not made by me.
George_George wrote: you create a base type variable (reference type) and a derived type variable (reference type), they are of different types but pointing to the same memory address
You have not said what you are assigning to these reference type variables. If it is one existing object that you are assigning to both objects then yes they will point to the same memory.
|
|
|
|
|
Thanks Colin!
Your reply is clear. Cool!
regards,
George
|
|
|
|
|
George_George wrote: I am doing input parameter checking for a string type, and string value except empty is valid. My function is provided to outer client to call. Sometimes I find to check null is not enough, I also need to check String.Empty.
Apart from your original question, you might also use String.IsNullOrEmpty[^] for this task.
regards
modified 12-Sep-18 21:01pm.
|
|
|
|
|
Thanks Greeeg,
Good to learn from you about the new API!
regards,
George
|
|
|
|
|
hi,
I have one table, inside this i have one server td.
Now i want to change the table cell background color dynamically i.e from the codebehind page.
If i am giving like this
First.BgColor = System.Drawing.Color.Green.ToString();
here First is my cell id.
But it is not effecting.
can anybody knows the solution, please let me know
Thanks and Regards
Dayakar D.N
|
|
|
|