C# Basics-Part 2(Namespaces,Properties,Constants and Read-Only Fields)


Namespaces

You would have recognized our class is wrapped in namespace block in our earlier lectures.
Namespaces are used to organize classes in separate containers, you can consider them as an address of your class that helps the compiler to find the class.














They are automatically added around the class and usually has the same name as the project they belong

Here we can see Class1 is inside namespace NameSpaceDemo
Imagine your application has multiple Class1 classes and now how do you guide the compiler on which one to pick, namespaces helps here.

You can access the class using Namespace then dot the class name.

NameSpaceDemo.Class1

Below one of the built-in namespace

System.Console.WriteLine("Test Namespace");

Here System is the Namespace
The console is the Class name
WriteLine is the method name

Properties

Properties allow you to control the accessibility of class variables.
Can be also called as guard access to the fields.

We will stick to our earlier example of DriverManager class, now suppose you have a requirement which says that one can only set the browser type during instance creation.

There are two ways you can achieve this

1. Marking browser Type variable as private and defining a public method to get the browser Type
2. Use properties, refer below code



Here we defined a property for browserType and marked the set property accessor to private, this makes it accessible only in the same class

public string browserType { getprivate set; }

We set the browser type during instance creation using parameterized constructor
public DriverManager(string browserType)
       {
           this.browserType = browserType;
       }

We can access the properties using the ObjectName.PropertyName
Console.WriteLine("BrowserType: " + driverManager.browserType);

Also, we will get an error if we try to set the browserType from outside the DriverManagerClass





This way we have met our requirement

Constants

Constant members of a class hold a value that does not change.
Also called as a compile time constant
They are accessed via the class name

Now let's say we know the browser version of the class beforehand and we know we will not change the version during runtime or we want to restrict other classes in doing so.

We can define and initialize browser version variable and prefix it with const keyword.

public const int browserVersion= 10;

We can access the same using the class name

Console.WriteLine(DriverManager.browserVersion);


ReadOnly Fields

Read-only fields hold a value that is initialized and then not changed.
Must be initialized in the declaration or in a constructor.
Called runtime constant value.

Requirement for DriverManager Class
Let's say, we don't know the browser version beforehand or we are depended on other class to give us the same.
Also, the browser version once set will not change.

Here we cant use a constant as we have to get the browser version at runtime, we can solve this using read-only variable.




We can also initialize the browser version variable in the constructor, see below code



































To summarize the difference between constants and read-only fields
  1. Constants are compile-time constants and read-only fields are runtime constants.
  2. Both are assigned to expression however in case of constants, it is evaluated at compile time and for read-only, it is evaluated at runtime.
  3. Constant assigned on a declaration and read-only can be assigned on declaration or constructor
  4. Constant can be of type number, boolean or string however read-only fields can be of any data type
  5. Constants are always static however read-only fields are optionally static.

Comments

Popular posts from this blog

Specflow -Part3(Working with tables using Specflow.Assist.Dynamic)

Specflow -Part4(Specflow Scenario Outline and Feature Background )

Specflow -Part6(Specflow Hooks)