C# Programming - Strings - Discussion

Discussion Forum : Strings - General Questions (Q.No. 7)
What will be the output of the C#.NET code snippet given below?
namespace IndiabixConsoleApplication
    class SampleProgram
        static void Main(string[ ] args)
            string str= "Hello World!";
            Console.WriteLine( String.Compare(str, "Hello World?" ).GetType() );
Hello World?
Answer: Option
No answer description is available. Let's discuss.
14 comments Page 1 of 2.

Joseph stalin said:   7 years ago
Anyone can explain how this logic works?

Suresh said:   9 years ago
Here the function returns 0 as the both strings are not equal. And the get type method will return the type of o as system.int32.

Gloops said:   9 years ago
Taylor, you have a simple way to know the value of an expression : while stopped at a breaking point on the following line, select the expression and depress shift F9 (this is in Visual Studio 2005). This also allows you to add a spy to watch the value dynamically.

I obtain -1, and if I replace the question mark by an exclamation mark to get the same text (and relaunch the application), I obtain 0.

Mithilesh Singh said:   9 years ago
String.Compare() returns 0 which is of type System.int32.

Max said:   9 years ago
Compare() methods always returns Int32 type because Int32 shows lexical relationship between the two comparands.

No matter what the string is, it will still return Int32 as answer.

For example :-

string str= null;
Console.WriteLine( String.Compare(str, null ).GetType());

Return : System.Int32

Note : Those who are thinking that first string will be compared and if matched/unmatched Boolean value will be returned and then it's type will be returned on the basis of 0/1. No such thing will happen here guyzzz !!.

Thank You :).

Ganesh said:   10 years ago
public static int Compare(string strA, string strB)
Member of System.String
return type of Compare method is int &

public Type GetType()

Return Value
Type: System.Type
The exact runtime type of the current instance.

AKS said:   1 decade ago
String comparison performs a case-sensitive comparison.

Assume s1 and s2 are two strings,

If s1 is equal to s2 then it returns zero.

If s1 is greater than s2 then it returns 1.

If s1 is less than s2 then it return -1.

Amrendra kumar said:   1 decade ago
Compare method has always return type is integer.

Taylor said:   1 decade ago
I'm just curious it about the result of the comparison, String.Compare(str, "Hello World?" ) , it it returned -1?

Can anyone please explain me this?

Swaroopa said:   1 decade ago
You are right Jay but its not System.System for string.Join...it is System.string...

Post your comments here:

Your comments will be displayed after verification.