user8396910
user8396910

Reputation:

Calling methods from properties in c#

I am currently learning c# and i am attempting to create a small application that uses SQLite as the database. After much research I was able to figure out how to create a connection, commands, parameters, etc. so everything is working fine. Part of my code is this:

    using var cmd = new SQLiteCommand();

        cmd.CommandText = "INSERT INTO Tasks (TaskName, TaskBody, TaskPriority) Values (@TaskName, @TaskBody, @Taskpriority)";
        cmd.Connection = con;
        cmd.Parameters.AddWithValue("@TaskName", Taskname);

I am confused as to why cmd.Parameters.AddwithValue works, because to my understanding cmd.Parameters is a property of SQLiteCommand class. AddwithValue() is a method of SQLiteParameterCollection class, so how is it that you can call a method from a property? What is that called? and how does it work in the background?
Thank you

Upvotes: 1

Views: 62

Answers (1)

KingOfArrows
KingOfArrows

Reputation: 556

I would recommend reading the docs on Properties.

A property is a wrapper of a field (which is a class level variable) that allows control of whether it can be read and written to. A property can be any type, just like how you can create a variable of any type. Parameters is a property of the class SqlCommand and is of type SqlParameterCollection. Since Parameters has a 'get' applied to it, you are able to get the SqlParameterCollection instance and view its contents and within the SqlParameterCollection class is the method AddWithValue

Upvotes: 4

Related Questions