Results 1 to 5 of 5

Thread: What is better (string)reader[0] or Convert.ToString(reader[0])

  1. #1
    Join Date
    Dec 2009
    Posts
    40

    What is better (string)reader[0] or Convert.ToString(reader[0])

    Hello friends,
    I recently started learning c-sharp language. I am confused about following method. I know that "var sis = (string)reader[0]" and "var sis = Convert.ToString(reader[0])" both are used for same function, but I don't know which method to use. Can anyone tell me what is better (string)reader[0] or Convert.ToString(reader[0])?
    Please help me.

  2. #2
    Join Date
    Nov 2005
    Posts
    1,323

    Re: What is better (string)reader[0] or Convert.ToString(reader[0])

    Both method do the same function, but as per my knowledge (string)reader[0] is better than Convert.ToString(reader[0]). It is very clearer and most probably faster than Convert.ToString(reader[0]), because in Convert.ToString(reader[0]) method first convert user input into string and then passed this value to variable. For this process it take more time than (string)reader[0].

  3. #3
    Join Date
    Oct 2005
    Posts
    2,393

    Re: What is better (string)reader[0] or Convert.ToString(reader[0])

    As per my knowledge var sis = (string)reader[0] gives you class cast exception if it can't be cast as a string and var sis = Convert.ToString(reader[0]) handle it more efficiently and you will get null if it can't be converted. This method also support more types of object for reader[0] where as other method will only allow casts where the type can be cast to a string.

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    Re: What is better (string)reader[0] or Convert.ToString(reader[0])

    I think "var s = (string)reader[0];" is better than "var s = Convert.ToString(reader[0]);" , because it is faster in execution. I also use these two methods in my program and I found that "var s = (string)reader[0];" is better than "var s = Convert.ToString(reader[0]) in execution. It is very fast and easy to use. As per my information var s = (string)reader[0]; will not give null value.

  5. #5
    Join Date
    Feb 2008
    Posts
    1,852

    Re: What is better (string)reader[0] or Convert.ToString(reader[0])

    As per my information it is depend on where we use these method in our program. If we want to read information from the user then we have to use Convert.ToString(reader[0]) method, because this method convert input into proper data and if we want to read input from any file then we can use (string)reader[0]. If we use these methods effectively then we can increase our program speed.

Similar Threads

  1. Replies: 3
    Last Post: 19-03-2012, 08:22 PM
  2. Convert string into int in C
    By screwball in forum Software Development
    Replies: 4
    Last Post: 22-12-2011, 08:47 PM
  3. Kindle vs Nook reader vs Sony reader
    By Bon-Z in forum Polls & Voting
    Replies: 5
    Last Post: 14-12-2009, 11:46 PM
  4. Replies: 4
    Last Post: 10-12-2009, 09:24 PM
  5. Replies: 1
    Last Post: 06-08-2009, 01:58 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,634,314.93484 seconds with 17 queries