My Journey to DLithe Bootcamp(.NET Full Stack Week 5(Feb 21-Feb 26)

Deepak Mishra
12 min readFeb 23, 2022

Assignment no : 30 DLithe_BC_NFS_T_Task30_C#

23/02/2022

Task Details:

  1. Collections
    2. Pass by value, reference, out
    3.Constructors and Destructors
    4. Abstract Classes
    5. Interfaces

What is Collections? Why we use collection in c#?

In C#, collection represents group of objects. By the help of collections, we can perform various operations on objects such as

  • store object
  • update object
  • delete object
  • retrieve object
  • search object, and
  • sort object

In sort, all the data structure work can be performed by C# collections.

We can store objects in array or collection. Collection has advantage over array. Array has size limit but objects stored in collection can grow or shrink dynamically.

Types of Collections in C#

There are 3 ways to work with collections. The three namespaces are given below:

  • System.Collections.Generic classes
  • System.Collections classes (Now deprecated)
  • System.Collections.Concurrent classes

a) System.Collections.Generic classes

The System.Collections.Generic namespace has following classes:

  • List
  • Stack
  • Queue
  • LinkedList
  • HashSet
  • SortedSet
  • Dictionary
  • SortedDictionary
  • SortedList

b) System.Collections classes

These classes are legacy. It is suggested now to use System.Collections.Generic classes. The System.Collections namespace has following classes:

  • ArrayList
  • Stack
  • Queue
  • Hashtable

c) System.Collections.Concurrent classes

The System.Collections.Concurrent namespace provides classes for thread-safe operations. Now multiple threads will not create problem for accessing the collection items.

The System.Collections.Concurrent namespace has following classes:

  • BlockingCollection
  • ConcurrentBag
  • ConcurrentStack
  • ConcurrentQueue
  • ConcurrentDictionary
  • Partitioner
  • Partitioner
  • OrderablePartitioner

Code Example of ArrayList with Operation

Add,Sorted ,removed

Output:

LinkedList Operations:

Code Example:

Code Output:

Dictionary :

code Example :

Code Output:

2.Pass by value, reference, out

C# ref vs out

Ref and out keywords in C# are used to pass arguments within a method or function. Both indicate that an argument/parameter is passed by reference. By default parameters are passed to a method by value. By using these keywords (ref and out) we can pass a parameter by reference.

Ref Keyword

The ref keyword passes arguments by reference. It means any changes made to this argument in the method will be reflected in that variable when control returns to the calling method.

code Example:

code Output:

Out Keyword

The out keyword passes arguments by reference. This is very similar to the ref keyword.

Code Example

3) Constructors :

Constructors and destructors are commonly used in most of your code. But there are certain concepts about them which most of you might not know. This article will highlight on those concepts and give you an overview about constructors and destructors using simple examples.

In this example, you have overloaded constructors in sampleClass and triggered constructors while instantiating the class. This is just a simple example. There are much more interesting characteristics about constructors. They are mentioned below:

• Even if no constructors are defined, default constructor will be executed internally
• From one constructor, you can call another constructor
• Constructors cannot be inherited
• From derived class constructor, you can call base class constructor
• Constructors can be overloaded as shown in the example above
• Constructors of a class can be classified into private constructors, instance constructors and static constructors. The example shown above is an example of instance constructor
• Constructors should not have a return type since they are already returning instance of the class

code example:

Code Output:

You already know that garbage collector does the cleanup for you. But they deal with only managed resources. If your object is accessing any unmanaged resource, then you can release then inside the destructor. A destructor can be defined in a class as shown below:

Destructor:

Constructors and Destructors — Order of Execution during Inheritance:

An interesting feature about constructors and destructors is their order of execution when they are involved in inheritance. Consider the following example which deals with constructors and destructors in multilevel inheritance:

Code Output:

4) Abstract Classes

Abstraction is an important part of object oriented programming. It means that only the required information is visible to the user and the rest of the information is hidden.

Abstraction can be implemented using abstract classes in C#. Abstract classes are base classes with partial implementation. These classes contain abstract methods that are inherited by other classes that provide more functionality.

Some of the salient points about abstract classes are as follows:

  1. The abstract class is created using the keyword abstract and some of the methods of the abstract class also contain the keyword abstract.
  2. No object can be created of the abstract class i.e.it cannot be instantiated.
  3. The abstract methods in the abstract class are implemented actually only in the derived classes.
  4. If all the methods in the abstract class contain the keyword abstract, then that class is known as pure Abstract class.

A program that demonstrates abstraction is given as follows:

Code Example:

Code Output:

5. Interfaces

Interface in C# is a blueprint of a class. It is like abstract class because all the methods which are declared inside the interface are abstract methods. It cannot have method body and cannot be instantiated.

It is used to achieve multiple inheritance which can’t be achieved by class. It is used to achieve fully abstraction because it cannot have method body.

Its implementation must be provided by class or struct. The class or struct which implements the interface, must provide the implementation of all the methods declared inside the interface.

C# interface example

Let’s see the example of interface in C# which has draw() method. Its implementation is provided by two classes: Rectangle and Circle.

Code Output:

Github Link:

Assignment No : 32 DLithe_BC_NFS_T_Task32_C#

23/02/2022

1. What is Virtual Functions in c#?

The virtual keyword is useful in modifying a method, property, indexer, or event. When you have a function defined in a class that you want to be implemented in an inherited class(es), you use virtual functions. The virtual functions could be implemented differently in different inherited class and the call to these functions will be decided at runtime.

The following is a virtual function

public virtual int area() { }

Code Example:

Code Output:

2. What is Generics in c# ?

I am going to discuss how to implement Generics in C# with examples. Please read our previous article where we discussed the Generic Collection in C#. As part of this article, we are going to discuss the following pointers.

Why do we need Generics in C#?

Let us understand the need for Generics in C# with one example. Let us create a simple program to check whether two integer numbers are equal or not. The following code implementation is very straightforward. Here we created two classes with the name ClsCalculator and ClsMain. Within the ClsCalculator class, we have AreEqual() method which takes two integer values as the input parameter and then it checks whether the two input values are equal or not. If both are equal then it returns true else it will return false. And from the ClsMain class, we are calling the static AreEqual() method and showing the output based on the return value.

Boxing and Unboxing

.Net defines two major categories of data type termed value type and reference type to represent a variable. This is where boxing and unboxing are needed. Boxing is a mechanism to explicitly convert a value type to a reference type by storing the variable into System.Object; when you box the value the CLR allocates a new object into the heap and copies the value type’s value into that instance. For example, you have created a variable of int type as:

int a = 20;

object b = a; //boxing

The opposite operation is Unboxing which is the process of converting back the reference type into the value type. This process verifies that the receiving data type is equivalent to the boxed type as

int c = (int)b; // unboxing

The C# compiler sees the assignment from int to object and vice-versa. When this program is compiled and you examine the IL generated code via IL dissembler, you notice that the program respond by inserting a box instruction in the IL automatically when b is assigned the value of a and an unbox instruction when c is assigned the value b as in the following;

Generic Classes

The Generic class can be defined by putting the <T> sign after the class name. It isn’t mandatory to put the “T” word in the Generic type definition. You can use any word in the TestClass<> class declaration.

public class TestClass<T> { }

The System.Collection.Generic namespace also defines a number of classes that implement many of these key interfaces. The following table describes the core class types of this namespace.

Simple Generic Class Example

The following example shows a simple Generic type manipulation. The TestClass<T> defines an array of generic type with length 5. The Add() method is responsible for adding any type of objects into the collection and the Indexer property is an implementation of foreach statement iteration. Finally in the main class we instantiated the TestClass<T> class with an Integer type reference and adds some integer type elements into the collection using the Add() method.

There are some significant characteristics of Generic types that make them special to the conventional non-generics type as follows;

  • Type Safety
  • Performance
  • Binary Code reuse

3. Files

A file is a collection of data stored in a disk with a specific name and a directory path. When a file is opened for reading or writing, it becomes a stream.

The stream is basically the sequence of bytes passing through the communication path. There are two main streams: the input stream and the output stream. The input stream is used for reading data from file (read operation) and the output stream is used for writing into the file (write operation).

C# I/O Classes

The System.IO namespace has various classes that are used for performing numerous operations with files, such as creating and deleting files, reading from or writing to a file, closing a file etc.

The following table shows some commonly used non-abstract classes in the System.IO namespace −

BinaryReader

Reads primitive data from a binary stream.

2 BinaryWriter

Writes primitive data in binary format.

3 BufferedStream

A temporary storage for a stream of bytes.

4 Directory

Helps in manipulating a directory structure.

5 DirectoryInfo

Used for performing operations on directories.

6 DriveInfo

Provides information for the drives.

7 File

Helps in manipulating files.

8 FileInfo

Used for performing operations on files.

9 FileStream

Used to read from and write to any location in a file.

10 MemoryStream

Used for random access to streamed data stored in memory.

11 Path

Performs operations on path information.

12 StreamReader

Used for reading characters from a byte stream.

13 StreamWriter

Is used for writing characters to a stream.

14 StringReader

Is used for reading from a string buffer.

15 StringWriter

Is used for writing into a string buffer.

The FileStream Class

The FileStream class in the System.IO namespace helps in reading from, writing to and closing files. This class derives from the abstract class Stream.

You need to create a FileStream object to create a new file or open an existing file. The syntax for creating a FileStream object is as follows −

Appending the value inside the Details .txt file:

Output File:

Github:

BACKEND PROGRAMMING LANGUAGES ASSIGNMENTS

Backend is the server-side of the software that stores and analyzes data, as well as ensuring smooth application performance. Backend developers take on a range of duties, such as writing APIs, libraries and working with system components, business processes, and data architecture.

Assignment no : 33 DLithe_BC_NFS_T_Task33_SQLSERVER

26/02/2022

Task Details:

1.DDL
2.DML
3.CONSTRAINTS
4.HOW TO GIVE USER-DEFINED CONSTRAINT NAMES
5.MULTIPLE ROWS IN ONE INSERT QUERY

1) What is DDL — Data Definition Language?

Structured Query Language(SQL) as we all know is the database language by the use of which we can perform certain operations on the existing database and also we can use this language to create a database. SQL uses certain commands like Create, Drop, Insert, etc. to carry out the required tasks.

DDL or Data Definition Language actually consists of the SQL commands that can be used to define the database schema. It simply deals with descriptions of the database schema and is used to create and modify the structure of database objects in the database.DDL is a set of SQL commands used to create, modify, and delete database structures but not data. These commands are normally not used by a general user, who should be accessing the database via an application.

List of DDL commands:

  • CREATE: This command is used to create the database or its objects (like table, index, function, views, store procedure, and triggers).
  • DROP: This command is used to delete objects from the database.
  • ALTER: This is used to alter the structure of the database.
  • TRUNCATE: This is used to remove all records from a table, including all spaces allocated for the records are removed.
  • COMMENT: This is used to add comments to the data dictionary.
  • RENAME: This is used to rename an object existing in the database.

2.What is DML ?

DML(Data Manipulation Language):

The SQL commands that deals with the manipulation of data present in the database belong to DML or Data Manipulation Language and this includes most of the SQL statements. It is the component of the SQL statement that controls access to data and to the database. Basically, DCL statements are grouped with DML statements.

List of DML commands:

  • INSERT : It is used to insert data into a table.
  • UPDATE: It is used to update existing data within a table.
  • DELETE : It is used to delete records from a database table.
  • LOCK: Table control concurrency.
  • CALL: Call a PL/SQL or JAVA subprogram.
  • EXPLAIN PLAN: It describes the access path to data.

3) what is SQL Constraints ?

SQL constraints are used to specify rules for the data in a table.

Constraints are used to limit the type of data that can go into a table. This ensures the accuracy and reliability of the data in the table. If there is any violation between the constraint and the data action, the action is aborted.

Constraints can be column level or table level. Column level constraints apply to a column, and table level constraints apply to the whole table.

The following constraints are commonly used in SQL:

  • NOT NULL - Ensures that a column cannot have a NULL value
  • UNIQUE - Ensures that all values in a column are different
  • PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely identifies each row in a table
  • FOREIGN KEY - Prevents actions that would destroy links between tables
  • CHECK - Ensures that the values in a column satisfies a specific condition
  • DEFAULT - Sets a default value for a column if no value is specified
  • CREATE INDEX - Used to create and retrieve data from the database very quickly

Github :

https://github.com/deepaksmishra/DLithe_DotnetFSD_JanFeb2022/tree/main/Deepak%20Mishra/Backend/SQL/DLithe_BC_NFS_T_Task33_SQLSERVER

--

--