Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C# Data Types
  • C# Decision Making
  • C# Methods
  • C# Delegates
  • C# Constructors
  • C# Arrays
  • C# ArrayList
  • C# String
  • C# Tuple
  • C# Indexers
  • C# Interface
  • C# Multithreading
  • C# Exception
Open In App
Next Article:
StringBuilder.EnsureCapacity() Method in C#
Next article icon

How to find the Capacity of a StringBuilder in C#

Last Updated : 26 Sep, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

StringBuilder.Capacity Property is used to get or set the maximum number of characters that can be contained in the memory allocated by the current instance.
 

Syntax: public int Capacity { get; set; }
Return Value: This property will return the maximum number of characters that can be contained in the memory allocated by the current instance. Its value can range from Length to MaxCapacity.
Exception: This property will give ArgumentOutOfRangeException if the value specified for a set operation is less than the current length of this instance or the value specified for a set operation is greater than the maximum capacity. 
 

Below programs will illustrate the use of the above-discussed property:
Example 1:
 

csharp




// C# program to demonstrate
// the Capacity() Property
using System;
using System.Text;
 
class GFG {
 
    // Main Method
    public static void Main(String[] args)
    {
 
        // create a StringBuilder object,
        // default capacity will be 16
        StringBuilder str = new StringBuilder();
 
        // get default capacity
        int cap = str.Capacity;
 
        Console.WriteLine("Default Capacity of StringBuilder = "
                                                         + cap);
 
        // add the String to StringBuilder Object
        str.Append("Geek");
 
        // get capacity
        cap = str.Capacity;
 
        // print the result
        Console.WriteLine("StringBuilder = " + str);
        Console.WriteLine("Current Capacity of StringBuilder = "
                                                         + cap);
    }
}
 
 
Output: 
 Capacity of StringBuilder = 16 StringBuilder = Geek Current Capacity of StringBuilder = 16

 

Example 2:
 

csharp




// C# program to demonstrate
// the Capacity() Property
using System;
using System.Text;
 
class GFG {
    public static void Main(String[] args)
    {
 
        // create a StringBuilder object
        // with a String passed as parameter
        StringBuilder str =
           new StringBuilder("WelcomeGeeks");
 
        // get capacity
        int capacity = str.Capacity;
 
        // print the result
        Console.WriteLine("StringBuilder = " + str);
        Console.WriteLine("Capacity of StringBuilder = "
                                            + capacity);
    }
}
 
 
Output: 
StringBuilder = WelcomeGeeks Capacity of StringBuilder = 16

 

Reference: 

  • https://docs.microsoft.com/en-us/dotnet/api/system.text.stringbuilder.capacity?view=netframework-4.7.2


Next Article
StringBuilder.EnsureCapacity() Method in C#

K

Kirti_Mangal
Improve
Article Tags :
  • C#
  • CSharp-StringBuilder-Class

Similar Reads

  • How to find the MaxCapacity of a StringBuilder in C#
    StringBuilder.MaxCapacity Property is used to get the maximum capacity of this instance. Syntax: public int MaxCapacity { get; } Property Value: It returns the maximum number of characters of type System.Int32 this instance can hold. Note: The maximum capacity for this implementation is Int32.MaxVal
    1 min read
  • How to create the StringBuilder in C#
    StringBuilder() constructor is used to initialize a new instance of the StringBuilder class which will be empty and will have the default initial capacity. StringBuilder is used to represent a mutable string of characters. Mutable means the string which can be changed. So String objects are immutabl
    2 min read
  • How to find the length of the StringBuilder in C#
    StringBuilder.Length Property is used to get or set the length of the current StringBuilder object. Syntax: public int Length { get; set; } It returns the length of the current instance. Exception: This property will give ArgumentOutOfRangeException if the value specified for a set operation is less
    2 min read
  • How to remove all characters from StringBuilder in C#
    StringBuilder.Clear Method is used to remove all the characters from the current StringBuilder instance. Syntax: public System.Text.StringBuilder Clear (); It returns an StringBuilder object whose length will be zero. Note: Clear is a convenience method that is equivalent to setting the Length prope
    1 min read
  • StringBuilder.EnsureCapacity() Method in C#
    The EnsureCapacity(Int32) method of StringBuilder class helps us to ensures the capacity is at least equal to the specified value that is passed as the parameter to the method. If the current capacity is less than the capacity parameter, memory for this instance is reallocated to hold at least capac
    2 min read
  • C# | Creating StringBuilder having specified capacity
    StringBuilder(Int32) constructor is used to initialize a new instance of the StringBuilder class which will be empty and will have the specified initial capacity. StringBuilder is used to represent a mutable string of characters. Mutable means the string which can be changed. So String objects are i
    2 min read
  • How to create a StringCollection in C#
    StringCollection() constructor is used to initializing a new instance of the StringCollection class. StringCollection class is a new addition to the .NET Framework class library that represents a collection of strings. StringCollection class is defined in the System.Collections.Specialized namespace
    2 min read
  • StringBuilder.CopyTo Method in C#
    This method is used to copy the characters from a specified segment of this instance to a specified segment of a destination Char array. Syntax: public void CopyTo (int sourceIndex, char[] destination, int destinationIndex, int count); Parameters: sourceIndex: It is the starting position in this ins
    2 min read
  • How to find the length of an Array in C#
    Array.Length Property is used to get the total number of elements in all the dimensions of the Array. Basically, the length of an array is the total number of the elements which is contained by all the dimensions of that array. Syntax: public int Length { get; } Property Value: This property returns
    3 min read
  • How to create a StringDictionary in C#
    StringDictionary() constructor is used to initialize a new instance of the StringDictionary class which will be empty and will have the default initial capacity. StringDictionary is a specialized collection. This class comes under the System.Collections.Specialized namespace. It only allows string k
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences