C# Basics-Part 1(Class,Objects,Constructors)
In this lecture, we will talk about the basics of C#, topics which are needed to start our automation journey
Before we start I assume you have installed Visual Studio Community or a similar version
Class
We have already talked about this in our previous lecture, however, we will write some code and we will talk about the same
Before we start I assume you have installed Visual Studio Community or a similar version
Class
We have already talked about this in our previous lecture, however, we will write some code and we will talk about the same
A class is a template for creating objects
We will stick to our earlier example of DriverManager class for managing the browser
namespace Configurations
{
/// Author- Amit Tambe
/// Purpose- This class manages interaction with the browser
class DriverManager
{
string browserType;
public void Launch()
{
//Launch browser code goes here
}
}
}
Here we created a class with name DriverManager
Here browserType variable holds string data and is called the class variable.
Launch method launches the browser and this is the operation performed by the class
In general, we can call a class as specification for a set of data and operations that act on data
Best practices
- A class should have meaning full names (use noun).
- Good to add comments above the class
- A class should have a single responsibility
- Use a verb(action) for method names(example GetDriver,SetDriver etc)
Objects
Objects are an instance of a class.
Classes are templates for objects and we can create as many objects from the same template
Consider you have a class called Car which holds details of the car like color, type, manufacturer etc
You can create separate objects of a class having different values sharing the same class template
Now how do we create an instance of a class, we will use our DriverManager class here
namespace Configurations { /// <summary> /// Author- Amit Tambe /// Purpose- This class manages interaction with the browser /// </summary> class DriverManager { public string browserType; public int browserVersion { get; set; } public void Launch() { //Launch browser code goes here } } class Main { public static void main(string[] args) {
DriverManager driverManager = new DriverManager();
driverManager.browserType = "IE"; } } }
Here we created an object of DriverManager class using a new keyword and called it driverManager
We can now access the members of the class(browserType) using dot keyword on an object name and set object specific values
Constructors
Constructors are special methods in a class that is called when an instance is created.
Usually, used to initialize the data members of a new object.
In the above code when we created the instance of DriverManager class default constructor is called.
Here we have not defined any constructor, internally a default constructor is generated by the compiler.
To summarize about constructor
- A special method in class
- Executed when an instance is created
- Name same as the class name
- The default constructor has no parameters
- The constructor if not defined, the compiler will generate a default constructor for you
- Has no return Type
Now suppose we want to build our DriverManager class which as below 2 requirements
- The caller can set the browserType
- Caller if doesn't set the browserType then default browser type should be set
We can meet these requirements using constructors, see below code
namespace Configurations { /// <summary> /// Author- Amit Tambe /// Purpose- This class manages interaction with the browser /// </summary> class DriverManager { string browserType_Default = "IE"; public string browserType;
public DriverManager() { this.browserType = browserType_Default; } public DriverManager(string browserType) { this.browserType = browserType; }
public void Launch() { //Launch broser code goes here } } class Main { public static void main(string[] args) {
DriverManager driverManager = new DriverManager(); DriverManager driverManager1 = new DriverManager("Firefox");
} } }
Here we created 2 constructor
- Default having no parameters
- One taking a single parameter
In the main class, when objects are created, their respective constructor is called
- When a driverManager object is created, the default constructor is called and browserType is set to a browserType_Default value (IE)
- When a driverManager1 object is created, another constructor is called and browserType is set to one which was passed during instance creation (Firefox)
this keyword is used to refer to the current instance of the class, usually used to differentiate between method parameters and class fields if they have the same name(Check 2nd constructor)
Constructor chaining
Used for calling one constructor from another constructor.
Well, you must be thinking why we need to do so?
Suppose your class is having multiple constructors and then you figured out that some of the code is repeated in each constructor.
Now how to refactor that ....
Move all the common code to base constructor and let each constructor call the base constructor
Show me how to do that
namespace Configurations { /// <summary> /// Author- Amit Tambe /// Purpose- This class manages interaction with the browser /// </summary> class DriverManager { string browserType_Default = "IE"; public string browserType; public DriverManager() { //Default/Base constructor code this.browserType = browserType_Default;
//Common code shared by each constructor goes here
} public DriverManager(string browserType)
:this()
{ //Constructor1 code this.browserType = browserType; } public DriverManager(string browserType, string browserversion)
:this()
{ //Constructor2 code } public void Launch() { //Launch browser code goes here } } class Main { public static void main(string[] args) { DriverManager driverManager = new DriverManager(); DriverManager driverManager1 = new DriverManager("Firefox"); } } }
You can now see, for both the constructor I have added this() to the method signature, it will invoke the default constructor first and then execute the constructor
Easy, we saved some code duplication 💪
Now we will move to the next lecture which will cover some more C# topics
Comments
Post a Comment