Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6331229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:01:23+00:00 2026-05-24T18:01:23+00:00

Sample code: DataTable table = new DataTable(); // … // insert column to table

  • 0

Sample code:

    DataTable table = new DataTable();

    // ...
    // insert column to table

    table.Columns.Add("name");

    // ...
    // insert value to table

    foreach (DataRow row in table.Rows) {
         row["name"];
         row.Field<string>("name");     
    }

My question is:

  • Is there a difference between using row["name"] and row.Field<string>("name")? Of course, the second way cast value to some type, but is there another difference?
  • Which method is better to use?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T18:01:24+00:00Added an answer on May 24, 2026 at 6:01 pm

    See Remarks section, main differences described there:

    The DataSet class represents null values with the Value instance
    of the DBNull class. A Language-Integrated Query (LINQ) expression
    that accessed a column with a null value would generate a
    InvalidCastException at run time. Additionally, DataSet does not
    support nullable types. The Field method provides support for
    accessing columns as nullable types. If the underlying value in the
    DataSet is Value, the returned nullable type will have a value of
    null.

    If the value of the specified DataColumn is null and T is a
    reference type or nullable type, the return type will be null. The
    Field method will not return Value.

    The Field method does not perform type conversions. If type
    conversion is required, you should first obtain the column value by
    using the Field method. The column value should then be converted to
    another type.

    The last paragraph makes a point as I’ve often seen numbers stored as strings in database, therefore varchar to int conversion would be required on data retrieval, so in this case using DataColumn is better, e.g.:

    int test = row.Field<int>("Test"); // InvalidCastException
    int test = Convert.ToInt32(row["Test"]); // Works like a charm
    

    DataRowExtensions.Field<T> Method (DataRow, String) first appeared in .NET 3.5 and it “provides strongly-typed access to each of the column values in the specified row. The Field method also supports nullable types.”

    Afaik, row["name"] returns object, row.Field<string>("name") returns a String.
    We shouldn’t be comparing apples and pears, hence you should be asking what’s better:

    row["name"].ToString() vs row.Field<string>("name")
    and the answer is: they’re same.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've a simple code, here I add a row to my a datatable which
I'm having an issue with resizing columns in a YUI2 DataTable when the table
I have a rich:dataTable . I want to hide a row with this code:
Sample code that shows how to create threads using MFC declares the thread function
Sample code: public class Service1 { public int Service1() { .... } } public
Sample code <asp:Repeater> <ItemTemplate> <asp:ListView DataSource=<%# Container.DataItem.Items %> ... /> <asp:DataPager .... /> </ItemTemplate>
In sample code, I have seen this: typedef enum Ename { Bob, Mary, John}
Searching for some sample code for converting a point in WGS84 coordinate system to
Here is my sample code: from xml.dom.minidom import * def make_xml(): doc = Document()
Here is a sample code to retrieve data from a database using the yield

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.