Click here to Skip to main content
15,908,112 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,


I am getting value">100.00" in m ={prices[0].Trim()}

and I am getting value ">100.00" in n.

C#
string[] m = { prices[0].Trim() };
string[] n = new string[] {">100.00"};

if (m == n)
{
....
}
else
{
...
}



In the above if condition is true (m == n). but cursor does not go to if condition, it goes to else condition. how to fix this. why it is not going to if condition. please help me out. Thank you.
Posted
Updated 4-Dec-13 21:14pm
v2

Arrays are reference types; so when you write
C#
if (m == n)

you are indeed comparing references, which are two distinct objects in this context.

As they seems to only contain one element, you could write:
C#
if (m[0].Equals(n[0]))


If they contain more than one element, then you have to implement your own string arrays comparison, one that meets your needs.
 
Share this answer
 
Comments
christhuxavier 5-Dec-13 2:33am    
Hi phil . Thank you. i got solution by you.
phil.o 5-Dec-13 2:44am    
You're welcome :)
I invite you to also take a look at both other solutions; I never had to use Linq SequenceEqual() method, it may be worth the look.
christhuxavier 5-Dec-13 3:37am    
okay thank you for suggesting me.
If you are comparing 2 string arrays,you can use SequenceEqual of linq, check this out:


http://www.dotnetperls.com/sequenceequal[^]
 
Share this answer
 
v2
If you are using .NET FrameWork 3.5 or later, you can use the Linq 'SequenceEqual operator to test for the equality of all elements of two IEnumerables:
C#
string[] ary1 = { "1", "2", "3", "1" };
string[] ary2 = { "1", "2", "3", "1" };

// in some method:
bool isAryEqual = ary1.SequenceEqual(ary2);
If you are going to use 'SequenceEqual, I suggest you study the examples here: [^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900