C# Programming - Strings - Discussion

Discussion Forum : Strings - General Questions (Q.No. 7)
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() );
        }
    }
}
0
1
String
Hello World?
System.Int32
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
14 comments Page 1 of 2.

Max said:   1 decade 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 :).

Gloops said:   1 decade 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.

Ganesh said:   1 decade 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.

Jay said:   1 decade ago
In this type ,we write
string.compare and use GetType() method then we find----System.Int32
string.Equals and use GetType() method then we find---System.Boolean
string.Join and use GetType() method then we find----System.System

Always remember

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.

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?

Suresh said:   10 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.

Surekha said:   1 decade ago
I think it is because String.Compare () returns 0 or 1 which are nothing but of type int32.

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

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


Post your comments here:

Your comments will be displayed after verification.