Thursday, April 23, 2020

String and StringBuilder Classes

Strings are plays a major role in our programming world. To deal with these Strings we are using String and StringBuilder classes.
String
A string is a sequential collection of Unicode characters that is used to represent text. String is a class which belongs to the namespace System. String.
String Concatenation can be done using '+' opearator or String.Concat method.
String.Concat method concatenates one or more instances of String.
Sample code:
  1. string string1 = "Today is " + DateTime.Now.ToString ("D") + ".";  
  2. Console.WriteLine (string1);  
  3.   
  4. string string2 = "Hi " + "This is Jitendra ";  
  5. string2 += "SampathiRao.";  
  6. Console.WriteLine(string2);  
StringBuilder
StringBuilder is a class which belongs to the namespace System.Text. This class cannot be inherited.
In StringBuilder we are using Append() method.
Sample code:
  1. StringBuilder number = new StringBuilder (10000);  
  2. for (int i = 0; i<1000; i++)  
  3. {  
  4.     return Number.Append (i.ToString ());  
  5. }  
So where we can use these classes?
The answer is for simple String manipulations we can use String class. But the string manipulations are more it is better to use StringBuilder class.
Why the StringBuilder class is better for more string manipulations instead of String class?
The String object is immutable. Every time you use one of the methods in the System. String class, you create a new string object in memory, which requires a new allocation of space for that new object. In situations where you need to perform repeated modifications to a string, the overhead associated with creating a new String object can be costly. It's nothing but performance of the application might be decreased. So we are using StringBuilder in such cases.
StringBuilder object is mutable. The System.Text.StringBuilder class can be used when you want to modify a string without creating a new object. For example, using the StringBuilder class can boost performance when concatenating many strings together in a loop."

Differences between String and StringBuilder:
It belongs to String namespaceIt belongs to String. Text namespace
String object is immutableStringBuilder object is mutable
Assigning:String s= "something important";Assigning:
We can use '+' operator or Concat method to concatenate the strings.Here we are using Append method.
When string concatenation happens, additional memory will be allocated.Here additional memory will be allocated when the string buffer capacity exceeds only.

Difference Between Struct And Class In C#

A class is a user-defined blueprint or prototype from which objects are created. Basically, a class combines the fields and methods(member function which defines actions) into a single unit.

Example

// C# program to illustrate the
// concept of class
using System;
  
// Class Declaration
public class Author {
  
    // Data members of class
    public string name;
    public string language;
    public int article_no;
    public int improv_no;
  
    // Method of class
    public void Details(string name, string language,
                        int article_no, int improv_no)
    {
        this.name = name;
        this.language = language;
        this.article_no = article_no;
        this.improv_no = improv_no;
  
        Console.WriteLine("The name of the author is :  " + name
                          + "\nThe name of language is : " + language
                          + "\nTotal number of article published  " 
                          + article_no + "\nTotal number of Improvements:"
                          +" done by author is : " + improv_no);
    }
  
    // Main Method
    public static void Main(String[] args)
    {
  
        // Creating object
        Author obj = new Author();
  
        // Calling method of class
        // using class object
        obj.Details("Ankita", "C#", 80, 50);
    }
}
Output:
The name of the author is :  Ankita
The name of language is : C#
Total number of article published  80
Total number of Improvements: done by author is : 50

structure is a collection of variables of different data types under a single unit. It is almost similar to a class because both are user-defined data types and both hold a bunch of different data types.

Example:

using System;
// Defining structure
public struct Car
{
  
    // Declaring different data types
    public string Brand;
    public string Model;
    public string Color;
}
  
class GFG {
  
    // Main Method
    static void Main(string[] args)
    {
  
        // Declare c1 of type Car
        // no need to create an 
        // instance using 'new' keyword
        Car c1;
  
        // c1's data
        c1.Brand = "Bugatti";
        c1.Model = "Bugatti Veyron EB 16.4";
        c1.Color = "Gray";
  
        // Displaying the values
        Console.WriteLine("Name of brand: " + c1.Brand + 
                          "\nModel name: " + c1.Model + 
                          "\nColor of car: " + c1.Color);
    }
}
Output:
Name of brand: Bugatti
Model name: Bugatti Veyron EB 16.4
Color of car: Gray

Difference between Class and Structure

CLASSSTRUCTURE
Classes are of reference types.Structs are of value types.
All the reference types are allocated on heap memory.All the value types are allocated on stack memory.
Allocation of large reference type is cheaper than allocation of large value type.Allocation and de-allocation is cheaper in value type as compare to reference type.
Class has limitless features.Struct has limited features.
Class is generally used in large programs.Struct are used in small programs.
Classes can contain constructor or destructor.Structure does not contain parameter less constructor or destructor, but can contain Parameterized constructor or static constructor.
Classes used new keyword for creating instances.Struct can create an instance, with or without new keyword.
A Class can inherit from another class.A Struct is not allowed to inherit from another struct or class.
The data member of a class can be protected.The data member of struct can’t be protected.
Function member of the class can be virtual or abstract.Function member of the struct cannot be virtual or abstract.
Two variable of class can contain the reference of the same object and any operation on one variable can affect another variable.Each variable in struct contains its own copy of data(except in ref and out parameter variable) and any operation on one variable can not effect another variable.