Menu Close

Category: Blog

DIFFERENCE BETWEEN SYSTEM.STRING AND SYSTEM.TEXT.STRINGBUILDER

DIFFERENCE BETWEEN SYSTEM.STRING AND SYSTEM.TEXT.STRINGBUILDER

System.String is immutable  |||      StringBuilder is Mutable

As StringBuilder objects are mutable, they offer better performance than string objects of type
System.String, when heavy string manipulation is involved.
EXAMPLE OF SYSTEM.STRING  I.E STRING() METHOD
Stack       and       Heap
string                   c#
                            vidoe
                             tutorial
                             for
                             beginners


EXAMPLE  OF SYSTEM.TEXT.STRINGBUILDER
Stack       and       Heap
string                   c# video tutorial for beginners


using System;
using System.Text;
namespace ConsoleApplication4
{
    public class MainClass
    {
        public static void Main()
        {
            string userString = string.Empty;
            for(int i = 1; i <= 10000;i++ )
            {
                userString += i.ToString() + ” “;
                
            }
            Console.WriteLine(userString);
            Console.ReadLine();
            
            // using system.string  
            //string userstring = “C#”;
            //userstring += “Vidoe”;
            //userstring += “Tutorail”;
            //userstring += “for”;
            //userstring += “Beginners”;
            //Console.WriteLine(userString);
            //Console.ReadLine();
            //// using system.text.stringbuilder
            //StringBuilder userString = new StringBuilder(“C#”);
            //userString.Append(” Video”);
            //userString.Append(” Tutorial”);
            //userString.Append(” For”);
            //userString.Append(” Beginners”);
            //Console.WriteLine(userString.ToString());
            //Console.ReadLine();
        }
    }
}
   


Share this:

DIFFERENCE BETWEEN CONVERT.TOSTRING() AND TOSTRING()

DIFFERENCE BETWEEN CONVERT.TOSTRING() AND TOSTRING()

Convert.Tostring() handles null, while ToString() doesn’t  and throws a NULL
Reference exception.

Depending on the type of the application architecture and what you are trying to achieve , 
you choose one over the other.

using System;
namespace ConsoleApplication4
{
    public class MainClass
    {
        public static void Main()
        {
            Customer C1 = null;
            string str = Convert.ToString(C1);
            Console.WriteLine(str);
            Console.ReadLine();
        }
    }
    public class Customer
    {
        public string Name { get; set; }
      
      
    }
}
   

Share this:

WHY SHOULD YOU OVERRIDE EQUALS METHOD

WHY SHOULD YOU OVERRIDE EQUALS METHOD

1. WHAT IS EQUALS METHOD

2. WHY SHOULD YOU OVERRIDE IT.

using System;

namespace ConsoleApplication4
{
    public class MainClass
    {
        public static void Main()
        {
            Customer C1 = new Customer();
            C1.FirstName = “Malla”;
            C1.LastName = “Gurram”;

            Customer C2 = new Customer();
            C2.FirstName = “Malla”;
            C2.LastName = “Gurram”;


            Console.WriteLine(C1 == C2);
            Console.WriteLine(C1.Equals(C2));
            Console.ReadLine();
        }

    }

    public class Customer
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }

        public override bool Equals(object obj)
        {
           
            if(obj == null)
            {
                return false;
            }

            if(!(obj is Customer))
            {
                return false;
            }

            return this.FirstName == ((Customer)obj).FirstName &&
                this.LastName == ((Customer)obj).LastName;
        }

        public override int GetHashCode()
        {
            return this.FirstName.GetHashCode() ^ this.LastName.GetHashCode();
        }
    }
}

   
Share this:

WHY SHOULD WE OVERRIDE TOSTRING METHOD

WHY SHOULD WE OVERRIDE TOSTRING METHOD

1. What is ToString() Method.

2. Why should we override it ?


using System;
using System.Reflection;
namespace ConsoleApplication4
{
    public class MainClass
    {
        public static void Main()
        {
            int Number = 10;
            Console.WriteLine(Number.ToString());
            Console.ReadLine();
            Customer C1 = new Customer();
            C1.FirstName = “Malla”;
            C1.LastName = “Gurram”;
            //Console.WriteLine(C1.ToString());   
            Console.WriteLine(Convert.ToString(C1));
            Console.ReadLine();
        }
    }
    public class Customer
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public override string ToString()
        {
            return this.LastName + “, ” + this.FirstName;
        }
    }
}
   

Share this:

REFLECTION WITH SAMPLE EXAMPLE IN C#

REFLECTION WITH SAMPLE EXAMPLE IN C#


This is a simple windows application to display the methods, properties and constructors using “Reflections” in c#


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Reflection;
namespace ReflectionDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }   

        private void btnDiscoverTypeInformation_Click_Click(object sender, EventArgs e)
        {
            string TypeName = txtTypeName.Text;

            

            Type  T = Type.GetType(TypeName);
                            lstMethods.Items.Clear();
            lstProperties.Items.Clear();
            lstConstructors.Items.Clear();

            MethodInfo[] methods = T.GetMethods();


            foreach(MethodInfo  method in methods)

            {
                lstMethods.Items.Add(method.ReturnType.Name + ” ” + method.Name);
                
            }


            PropertyInfo[] properties = T.GetProperties();           
            foreach (PropertyInfo property in properties)
            {
                lstProperties.Items.Add(property.PropertyType.Name + ” ” + property.Name);

            }


            ConstructorInfo[] constructors = T.GetConstructors();
            foreach (ConstructorInfo constructor in constructors)
            {
                lstConstructors.Items.Add(constructor.ToString());

            }
        }

            
    }
}

Share this: