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.

Juhi said:   1 decade ago
Can anyone explain me this.

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

Harshil said:   1 decade ago
I think surekha is right.

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

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

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?

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

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.

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.

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 :).


Post your comments here:

Your comments will be displayed after verification.