简体   繁体   中英

DataRow and the protected internal constructor

How can a DataTable create a new instance of a DataRow with the NewRow method if the constructor of the DataRow class is protected internal and DataTable doesn't inherit from DataRow?

Example:

class Program
{
    static void Main()
    {
        // error: inaccessible due to its protection level
        DataRow dr = new DataRow(); 

        // works
        DataRow dr = new DataTable().NewRow();
    }
}

protected internal means "accessible by derived classes" and "accessible by other classes in the same assembly". DataTable and DataRow are in the same assembly, so DataTable has access to all of DataRow 's internal members.

Hope you already got an answer for this.

But still I am adding my answer to this to address “Why it is designed in this way”.

As “hvd” mentioned they are in same assembly that's why DataTable able to create instance of DataRow.

The reason for this approach is:

• Data row contains values for each column • Ideally an array internally used to store these values

• So each data row contains array which contains values

• But data row will not be knowing the size of the array to initialize

• Which depends on number of columns in the data.table

• But data.table knows how many columns in the table

• That's why it takes responsibility to create OR set the array size of the DataRow

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM