本文整理汇总了C#中StringBuilder.EnsureCapacity方法的典型用法代码示例。如果您正苦于以下问题:C# StringBuilder.EnsureCapacity方法的具体用法?C# StringBuilder.EnsureCapacity怎么用?C# StringBuilder.EnsureCapacity使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类StringBuilder
的用法示例。
在下文中一共展示了StringBuilder.EnsureCapacity方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
public static void Main( string[] args )
{
StringBuilder buffer =
new StringBuilder( "Hello, how are you?" );
// use Length and Capacity properties
Console.WriteLine( "buffer = " + buffer +
"\nLength = " + buffer.Length +
"\nCapacity = " + buffer.Capacity );
buffer.EnsureCapacity( 75 ); // ensure a capacity of at least 75
Console.WriteLine( "\nNew capacity = " +
buffer.Capacity );
// truncate StringBuilder by setting Length property
buffer.Length = 10;
Console.Write( "\nNew length = " +
buffer.Length + "\nbuffer = " );
// use StringBuilder indexer
for ( int i = 0; i < buffer.Length; i++ )
Console.Write( buffer[ i ] );
Console.WriteLine( "\n" );
}
示例2: LoadRenderModelNames
static string[] LoadRenderModelNames()
{
var results = new List<string>();
results.Add("None");
using (var holder = new SteamVR_RenderModel.RenderModelInterfaceHolder())
{
var renderModels = holder.instance;
if (renderModels != null)
{
uint count = renderModels.GetRenderModelCount();
for (uint i = 0; i < count; i++)
{
var buffer = new StringBuilder();
var requiredSize = renderModels.GetRenderModelName(i, buffer, 0);
if (requiredSize == 0)
continue;
buffer.EnsureCapacity((int)requiredSize);
renderModels.GetRenderModelName(i, buffer, requiredSize);
results.Add(buffer.ToString());
}
}
}
return results.ToArray();
}
示例3: GetOidValue
internal static string GetOidValue(IntPtr asn1ObjectPtr)
{
// OBJ_obj2txt returns the number of bytes that should have been in the answer, but it does not accept
// a NULL buffer. The documentation says "A buffer length of 80 should be more than enough to handle
// any OID encountered in practice", so start with a buffer of size 80, and try again if required.
StringBuilder buf = new StringBuilder(80);
int bytesNeeded = ObjObj2Txt(buf, buf.Capacity, asn1ObjectPtr);
if (bytesNeeded < 0)
{
throw CreateOpenSslCryptographicException();
}
Debug.Assert(bytesNeeded != 0, "OBJ_obj2txt reported a zero-length response");
if (bytesNeeded >= buf.Capacity)
{
int initialBytesNeeded = bytesNeeded;
// bytesNeeded does not count the \0 which will be written on the end (based on OpenSSL 1.0.1f),
// so make sure to leave room for it.
buf.EnsureCapacity(bytesNeeded + 1);
bytesNeeded = ObjObj2Txt(buf, buf.Capacity, asn1ObjectPtr);
if (bytesNeeded < 0)
{
throw CreateOpenSslCryptographicException();
}
Debug.Assert(
bytesNeeded == initialBytesNeeded,
"OBJ_obj2txt changed the required number of bytes for the realloc call");
if (bytesNeeded >= buf.Capacity)
{
// OBJ_obj2txt is demanding yet more memory
throw new CryptographicException();
}
}
return buf.ToString();
}
示例4: GetSid
public static string GetSid(string username, string domain)
{
byte [] Sid = null;
uint cbSid = 0;
StringBuilder referencedDomainName = new StringBuilder();
uint cchReferencedDomainName = (uint)referencedDomainName.Capacity;
SID_NAME_USE sidUse;
int err = NO_ERROR;
if (!LookupAccountName(null,username,Sid,ref cbSid,referencedDomainName,ref cchReferencedDomainName,out sidUse))
{
err = Marshal.GetLastWin32Error();
if (err == ERROR_INSUFFICIENT_BUFFER)
{
Sid = new byte[cbSid];
referencedDomainName.EnsureCapacity((int)cchReferencedDomainName);
err = NO_ERROR;
if (!LookupAccountName(null,username,Sid,ref cbSid,referencedDomainName,ref cchReferencedDomainName,out sidUse))
err = Marshal.GetLastWin32Error();
}
}
if (err == 0)
{
IntPtr ptrSid;
if (!ConvertSidToStringSid(Sid,out ptrSid))
{
err = Marshal.GetLastWin32Error();
return(null);
}
else
{
string sidString = Marshal.PtrToStringAuto(ptrSid);
LocalFree(ptrSid);
return(sidString);
}
}
else
return(null);
}
示例5: TestEnsureCapacity_Invalid
public static void TestEnsureCapacity_Invalid()
{
var builder = new StringBuilder("Hello", 10);
Assert.Throws<ArgumentOutOfRangeException>("capacity", () => builder.EnsureCapacity(-1)); // Capacity < 0
Assert.Throws<ArgumentOutOfRangeException>("capacity", () => builder.EnsureCapacity(builder.MaxCapacity + 1)); // Capacity > builder.MaxCapacity
}
示例6: TestEnsureCapacity
public static void TestEnsureCapacity()
{
var builder = new StringBuilder(40);
builder.EnsureCapacity(20);
Assert.True(builder.Capacity >= 20);
builder.EnsureCapacity(20000);
Assert.True(builder.Capacity >= 20000);
// Ensuring a capacity less than the current capacity does not change anything
int oldCapacity = builder.Capacity;
builder.EnsureCapacity(10);
Assert.Equal(oldCapacity, builder.Capacity);
}
示例7: LoadRenderModelNames
static string[] LoadRenderModelNames()
{
var results = new List<string>();
results.Add("None");
var error = HmdError.None;
if (!SteamVR.active)
{
OpenVR.Init(ref error);
if (error != HmdError.None)
return results.ToArray();
}
var pRenderModels = OpenVR.GetGenericInterface(OpenVR.IVRRenderModels_Version, ref error);
if (pRenderModels == System.IntPtr.Zero || error != HmdError.None)
{
if (!SteamVR.active)
OpenVR.Shutdown();
return results.ToArray();
}
var renderModels = new CVRRenderModels(pRenderModels);
uint count = renderModels.GetRenderModelCount();
for (uint i = 0; i < count; i++)
{
var buffer = new StringBuilder();
var requiredSize = renderModels.GetRenderModelName(i, buffer, 0);
if (requiredSize == 0)
continue;
buffer.EnsureCapacity((int)requiredSize);
renderModels.GetRenderModelName(i, buffer, requiredSize);
results.Add(buffer.ToString());
}
if (!SteamVR.active)
OpenVR.Shutdown();
return results.ToArray();
}