string的=运算符问题

chenwei1108 2012-02-14 11:32:25

string str1="abc";
string str2=str1;
Console.WriteLine(str1);
Console.WriteLine(str2);
str2="cba";
Console.WriteLine(str1);
Console.WriteLine(str2);

System.String继承于System.Object,使用赋值运算符时,.net framework到底做了什么?求解释.
...全文
99 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
微尘海 2012-02-14
  • 打赏
  • 举报
回复
string + 运算会从新分配内存
Saleayas 2012-02-14
  • 打赏
  • 举报
回复
[code=c#]
// ==++==
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// ==--==
/*============================================================
**
** Class: String
**
**
** Purpose: Contains headers for the String class. Actual implementations
** are in String.cpp
**
**
===========================================================*/
namespace System {
using System.Text;
using System;
using System.Runtime.ConstrainedExecution;
using System.Globalization;
using System.Threading;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using Microsoft.Win32;
using System.Runtime.InteropServices;
using va_list = System.ArgIterator;
//
// For Information on these methods, please see COMString.cpp
//
// The String class represents a static string of characters. Many of
// the String methods perform some type of transformation on the current
// instance and return the result as a new String. All comparison methods are
// implemented as a part of String. As with arrays, character positions
// (indices) are zero-based.
//
// When passign a null string into a constructor in VJ and VC, the null should be
// explicitly type cast to a String.
// For Example:
// String s = new String((String)null);
// Text.Out.WriteLine(s);
//
[System.Runtime.InteropServices.ComVisible(true)]
[Serializable] public sealed class String : IComparable, ICloneable, IConvertible, IEnumerable
#if GENERICS_WORK
, IComparable<String>, IEnumerable<char>, IEquatable<String>
#endif
{

//
//


[NonSerialized]private int m_arrayLength;
[NonSerialized]private int m_stringLength;
[NonSerialized]private char m_firstChar;

//private static readonly char FmtMsgMarkerChar='%';
//private static readonly char FmtMsgFmtCodeChar='!';
//These are defined in Com99/src/vm/COMStringCommon.h and must be kept in sync.
private const int TrimHead = 0;
private const int TrimTail = 1;
private const int TrimBoth = 2;

// The Empty constant holds the empty string value.
//We need to call the String constructor so that the compiler doesn't mark this as a literal.
//Marking this as a literal would mean that it doesn't show up as a field which we can access
//from native.
public static readonly String Empty = "";

...
}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.

[/code]
EnForGrass 2012-02-14
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 chenwei1108 的回复:]

System.String中用于保存字符串的字段是什么类型,存储在什么地方?求System.String的完整源码。
[/Quote]
String 对象是 System.Char 对象的有序集合,用于表示字符串。
看看MSDNhttp://msdn.microsoft.com/zh-cn/library/system.string(v=VS.80).aspx
gdstfox 2012-02-14
  • 打赏
  • 举报
回复
string 重载了运算符,两个字符串相加会重新分配内存空间。
chenwei1108 2012-02-14
  • 打赏
  • 举报
回复
System.String中用于保存字符串的字段是什么类型,存储在什么地方?求System.String的完整源码。

110,567

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧