String Intering in C# [duplicate] - c#

This question already has answers here:
String interning in .Net Framework - What are the benefits and when to use interning
(5 answers)
String Interning
(4 answers)
Why some identical strings are not interned in .NET?
(5 answers)
Can two identical strings be two separate instances in C#?
(3 answers)
Intern string literals misunderstanding?
(2 answers)
Closed 5 years ago.
Consider the following code snippet in C#
string s = "hi";
object k="hi".Substring(0);
// 1>
k==s // true
// 2>
Object.ReferenceEquals(s,k) //true
but when,
Object k="hii".Substring(0,2);
// 1>
k==s // false
// 2>
Object.ReferenceEquals(s,k) //false
I am having difficulty understanding why in the first case, the strings were interned while it doesn't happen so in the second case.
Also it would be very helpful if anyone can point out the rules when string interning happens in c#.

Related

C# convert string value to display with thousands separators [duplicate]

This question already has answers here:
How can I convert String to Int?
(31 answers)
String.Format an integer to use a thousands separator with decimal value in danish culture
(5 answers)
How would I separate thousands with space in C#
(6 answers)
Closed 2 years ago.
I'm receiving a string value (for example "331000110.00") and I want to convert it so it has thousands separators (I want to have something like this: 331 000 110.00). Important thing is that I want this field to stay as a string. Anyone can help me with this?

C# Generics, what does T<,> mean? [duplicate]

This question already has answers here:
Generics open and closed constructed types
(3 answers)
C# Language: generics, open/closed, bound/unbound, constructed
(2 answers)
Closed 3 years ago.
I have not seen this <,>-syntax before, and its impossible to google.
cfg.For(typeof(IPipelineBehavior<,>)).Add(typeof(OuterBehavior<,>));
.. I m guessing its some kind of wildcard, but I cant find any documentation
Source (for example)

Evaluate the string as math expression [duplicate]

This question already has answers here:
Evaluating string "3*(4+2)" yield int 18 [duplicate]
(13 answers)
Is there a string math evaluator in .NET?
(18 answers)
Closed 4 years ago.
I have one scenario in C#, which I fetch the expression from database, I need to parse and evaluate the same
The expression can be of any type (With + & - Operator only) as mentioned below
X+Y-Z
X-Y-Z
Z-Y+Z
Where as at run time I have any of above expression as string item and the values for each variable defined in it.
Now I need to automate the same, so that my code at run time will able to parse and evaluate the same.
I believe Switch case and if/else loop is the one way, but any can please suggest some other better and efficient way.
Thanks in advance

byte[] to string without knowing the coding [duplicate]

This question already has answers here:
How do I get a consistent byte representation of strings in C# without manually specifying an encoding?
(41 answers)
Determine a string's encoding in C#
(10 answers)
How to find out the Encoding of a File? C#
(5 answers)
Closed 9 years ago.
I saw this answer, The problem is - so I also need to know what type of encoder to use for getting the correct string - it may be in UTF\UTF8\ANSI.
Here is a example from the immediate window.
Encoding.Unicode.GetString(combinedBuf)
"믯ힿ힩힜힕�"
Encoding.UTF8.GetString(combinedBuf)
"שלום"

Equivalence of String and string in C# test [duplicate]

This question already exists:
Closed 11 years ago.
Possible Duplicate:
String vs string in C#
I have a test in C# code I'm reading:
if (variable is string)
I am wondering if this is strictly equivalent to:
if (variable is String)
or if some esoteric behavior of C# autoboxing may cause these tests to behave differently.
They are exactly the same - string is an alias for System.String.

Categories