English 中文(简体)
不可改变的原则在操纵str时是否仍然适用?
原标题:Does the immutable principle still apply when manipulating a char in a string?

如果我操纵了一条str子,它是否仍然被《刑法》视为在内部进行操纵,导致临时扼制?

例如:

string myString = "String";
myString[0] =  s ;

如何建立正在编辑的雕像的char阵列,并针对这个词进行所有具体操纵,将其改为扼杀。

它是否有助于至少削减2个实际操纵的诱惑?

问题回答

该法典没有编纂:

error CS0200: Property or indexer string.this[int] cannot be assigned to -- it is read only

这是因为正如你所说的那样,扼杀是不可改变的。

为了修改指示数,你需要使用<代码>StringBuilder类别(所有意图和目的的变式插图),该类别具有读写的<条码>。

StringBuilder builder = new StringBuilder("Sample");
builder[0] =  s ; //produces "sample"

或许值得指出的是,虽然在通常使用中,扼杀被认为是不可改变的,但实际上有可能变换。 如果你将VS项目混为“低劣的不安全守则”,那么你就可以使用不安全的法典来改变扼杀:

internal class Program
{
    private static void Main()
    {
        const string SomeString = "hello";

        Console.WriteLine("Original string: {0} .", SomeString);

        unsafe
        {
            fixed (char* charArray = SomeString)
            {
                byte* buffer = (byte*)(charArray);

                buffer[0] = 66;
                buffer[2] = 76;
                buffer[4] = 73;
                buffer[6] = 78;
                buffer[8] = 71;
            }
        }

        Console.WriteLine("Mutated string: {0} .", SomeString);
    }
}

这可被视为极恶,但它确实有其用途,例如。 定期用斜体而不是char子表示。

优势是不可改变的。 你们不能改变个人果园。 插图索引操作员只读写,因此如果你试图用阵列索引进行插图,就会出现错误。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签