Skip to main content

How to Create Dynamic Strings in C#

In this article, we shall see how to create dynamic strings in c#

In the above code, s1 is created and the changed, so old and new s1 to be stored in temporarily in memory. The old s1 will be cleared from memory by garbage collection process. If our application frequently changes the string, then obviously we may hold large memory in use, while waiting for the next garbage collection to clean up the memory.

Concatenating Strings:
The string.Concat method creates a new string, and concatenates with the result of the ToString method and then stores the result in new memory location, which is then linked to s.
This means, we have two string when we need only one. Also in real time scenario we may work with multiple strings, let’s say we had a situation to concatenate strings in a loop, this situation can be both a performance and memory problem
So to overcome this issue, we need to implement String.Builder class in System.Text namespace

When to use StringBuilder Class
If we concatenate string in for loop, where large number of string are stored in memory, then it is recommended to use StringBuilder Class.
StringBuilder also acts like the collection classes. It allocates an initial value of 16 characters and if your string becomes larger than this, it automatically grows to accommodate the string size.
We can implement string builder as below

    int amount =21;
    StringBuilder sb= new StringBuilder("My Balance is ");

Above code contains only one string, which is referenced by sb.
Methods of StringBuilder class:
There are 5 important methods in stringbuilder class as listed below
    a)     Append
    b)    AppendFormat
    c)     Insert
    d)    Remove
    e)     Replace
This method places an item at the end of current StringBuilder.
This method specifies a format for the object.
Places the object at specific index
As name indicates, Removes characters
As names indicates, Replaces characters.
Examples on Each method:
Let us create a object s as shown below
 StringBuilder sb= new StringBuilder(“ABCD”);
a) Way of Appending
 b) Output:
a)     Way to apply appendformat
            s.AppendFormat (“{0:n}”,1100);
     b)   Output:
      a)     Way to insert an object
      b) Output:
       a)     Way to Remove the object
     b) Output:
a)     Way to Replace an object
           b) Output:

Interview Question:
After execute in the following code, how can you preserver computer memory?
for(int i=0; i<1000;i++)

StringBuilder= new StringBuilder();
for(int i=0; i<1000;i++)

Thanks for reading this article, Sharing is caring J


Popular posts from this blog

ERROR: Cannot resolve the collation conflict between SQL_Latin1_General_CP1_CI_AS and Latin1_General_CI_AS_KS_WS in the equal to operation

ERROR: Cannot resolve the collation conflict between SQL_Latin1_General_CP1_CI_AS and Latin1_General_CI_AS_KS_WS in the equal to operation
dont get panic if u get the following error while in your joinings, there is a simple way to solve this.
it is because of collation settings on two columns we are doing joinings. it is because of collation settings on two columns we are doing joinings
First step is to figure out what are two colaltion that cause for conflicts. Let us assume that collation has not been set at the column level and instead at db level, then we have to execute two simple statements as below.

Statements: Select DATABASEPROPERTYYEX('DB1',N'Collation') Select DATABASEPROPERTYYEX('DB2',N'Collation')
One more thing, make a note that if you are on sharepoint you will get error as below
Latin_General_CI_AS_KS_WS. if you are on most any other database and use the dafault settings you may get this SQL_Latin_General_CP1_CI_AS.
Now we have to do some…

How To Create The Data Adapter Object Using ADO.NET by #RathrolaPremKumar

By reading this article, you will learn key points in ADO.NET, as given below. What is Data Adapter?Primary Data Adapters for the databases.Data Adapter properties.Methods used by Data Adapter.How to create Data Adapter with an example? Let’s start our demo with a Data Adapter introduction. Actually, we use Data Adapter object to establish the connection to the data source and manage the movement of date to and from the database. What is Data Adapter? A data adapter aobject serves as a bridge between a data set object and Data Source such as a database to retrieve and save the data. Data adapter contains a set of database commands and a database connection, which we use to fill a dataset object and update the Data Source. Primary data adapters for databases? .NET makes two primary data adapters available for use with the databases. Other data adapters can also be integrated with Visual Studio .NET. Primary Data Adapters are mentioned below. OleDbData Adapter, which is suitable for use wi…

'#Resolve' To export a list, you must have a Microsoft SharePoint Foundation-compatible application.

Searching this error message easily finds the root of the problem. There is a problem with the Office 64 bit version. It doesn’t work with SharePoint. These solutions generally tell you to uninstall your 64 bit office and install a 32 bit version. Happily there is a much easier solution.
Despite the error message the export actually works! Notice the yellow bar at the bottom of your browser. Simply save the .iqy file normally and open it with Excel. It’s that simple. The error message is certainly a nuisance but does not actually stop the export.
Thanks for ready my blog :)