Home / Articles / Programming Languages / C# / C# Arrays and Strings III / A Few Subtleties

“Chishiki” is Japanese for “knowledge.” e-chishiki.com aims to bring software developers, information security professionals, IT executives and other IT pros a rich body of knowledge in the form of articles, interviews, tutorials and technical discussions. Our contributors are among the biggest names in the Indian IT industry and include noted authors, educators and practitioners.

C# Programming Series

C# Arrays and Strings III

Yashavant Kanetkar
Yashavant Kanetkar

Index

  1. Strings
  2. A Few Subtleties

A Few Subtleties

Till now we have printed values using the WriteLine( ) method. What about taking the input from the user? The Console class provides a method ReadLine( ) that accepts a complete line entered (including spaces) by the user on the console. Then what about integers and floats? The same ReadLine( ) method can be used to accept numbers too. But it returns numbers in the form of a string object. The following program demonstrates how these numbers can be converted back to their numeric values:

using System ;
namespace sample
{
    class Class1
    {
        static void Main ( string[ ] args )
        {
            Console.WriteLine ( "Enter an integer: " ) ;
            string s = Console.ReadLine( ) ;
            int i = int.Parse ( s ) ;
            Console.WriteLine ( "Enter a float: " ) ;
            s = Console.ReadLine( ) ;
            float f = float.Parse ( s ) ;
            float t = f + i ;
            Console.WriteLine ( t ) ;
        }
    }
}

In this program firstly we have used the ReadLine( ) method to accept an integer from the console. Then using the int data type (which is an alias of Int32 structure defined by .NET) we have called a static method Parse( ) of the Int32 structure. This method converts a string to an integer. Likewise, to convert a string to a float we have called the static method Parse( ) of the structure Single (of which the float data type is an alias).

The numeric values can also be converted to strings. The following code snippet demonstrates how integers and floats can be converted to a string:

int i = 10 ;
float f = 3.14f ;
Console.WriteLine ( "Integer to String : " + i.ToString( ) ) ; 
Console.WriteLine ( "Float to String: " + f.ToString( ) ) ; 

Here ToString( ) method is a non-static method of the structures Int32 and Single.

There is one more way in which we can convert numbers to strings. This is as follows:

<span style="font-size: 11pt; line-height: 115%; font-family: "Calibri","sans-serif"; color: rgb(31, 73, 125);"></span>string s = string.Format ( "Value of i: {0} \nValue of f: {1}", i, f ) ;
Console.WriteLine ( s ) ;

Here the Format( ) method is a static method of the String class.

Comments

Log in or create a user account to comment.

On Sale From April 2008

Let Us C
8th Ed.
C programming classic & best seller. 1 million+ copies sold!

Y. Kanetkar

On Sale From April 2008

Introduction to Object Oriented Programming & C++

Y. Kanetkar

On Sale From Fall 2008

Microsoft .NET Framework: Web Application Security

Vijay Mukhi

On Sale From Nolvember 2008

Quest C++ Courseware
12+ hours of instructional audio and animated slides.

Y. Kanetkar Asang Dani

On Sale From November 2008

A Programmer's Guide to Web Application Security

Vijay Mukhi

Latest Forum Posts