ASP.NET: String VS string builder in ASP.NET (c#)

ASP.NET: String VS string builder in ASP.NET (c#)

Here I will explain the difference between string and stringbuilder in c#.

Description:


String

String is immutable object  (i.e.  once created cannot be changed ) and It always create new object of string type in memory.  If we want to append or replace something in string then it will discard the old value and will create new instance in memory to hold the new value. String belongs to “System” namespace.

 Example


string str = "Hello Visitor";
        // create a new string instance instead of changing the old one
        str += "How Are";
        str += "You ??";








  Stringbuilder

 StringBuilder is mutable, means if create string builder object then you can make modification in the data i.e. you can append and replace the string  without creating new instance for every time.it will update string at one place in memory doesn’t create new space in memory. Stringbuilder belongs to “System.Text”  namespace.

Example

StringBuilder strbuilder = new StringBuilder("");
        strbuilder.Append("Hello");
        strbuilder.Append("Visitor ");
        string str = sb.ToString();


Please mention in the comments in case you have any doubts related to the post:
Difference between string and stringbuilder. 
SQL: Explain CTE (Common Table expression) in sql databse?

SQL: Explain CTE (Common Table expression) in sql databse?

Common Table expression:

CTE stands for Common Table expressions. It was introduced with SQL Server 2005. It is a temporary result set which can be referenced in the same query just as a view. Unlike temporary table its life is limited to the current query. It is defined by using WITH statement. CTE improves readability and ease in maintenance of complex queries and sub-queries.

Description:


Syntax:

; WITH CTEName (Column aliases)
AS (Subquery)
SELECT statement
FROM CTEName;  


Example:

Complex query Using sub-query:

SELECT * FROM (
SELECT Dept.Department, Emp.Name, Emp.Age From Department Dept
Inner join Employee Emp on Emp.EID = Dept.EID) Temp
WHERE Temp.Age > 40
ORDER BY Temp.NAME


By Using CTE:

;With CTE1(Department, Name, Age)--Column names
AS
(
SELECT Dept.Department, Emp.Name, Emp.Age from Department Dept
INNER JOIN EMP Emp ON Emp.EID = Dept.EID
)
SELECT * FROM CTE1 --Using CTE
WHERE CTE1.Age > 40
ORDER BY CTE1.NAME


When to Use CTE

1.       This is a substitute of complex sub query.
2.       Substitute for a view when the general use of a view is not required.
3.       This is also used to create a recursive query or non-recursive query.

4.       We can also update data using CTE (Common table expression). 


Kategori

Kategori