String是一种引用类型,即使它具有值类型的大多数特性,例如是不可变的并且具有==重载以比较文本而不是确保它们引用相同的对象.
为什么字符串不是一个值类型呢?
我正在努力如何在真正的应用程序中使用"ref"(通过引用传递参数).我想有一个简单而有意义的例子.到目前为止我发现的所有内容都可以通过向方法添加返回类型来轻松重做.有人在想吗?谢谢!
我想使用 [ref] 关键字引用数组中的单个元素。
如何测试参考:
$var1 = "this is a string"
[ref]$var2 = [ref]$var1
$var2.Value
this is a string
$var2.Value += " too!"
$var2.Value
this is a string too!
$var1
this is a string too!
Run Code Online (Sandbox Code Playgroud)
以上按预期工作。但是现在要引用任何数组中的单个元素?
$var3="string 1", "string 2", "string 3"
[ref]$var2=[ref]($var3[1])
$var2.Value
string 2
$var2.Value += " updated!"
$var2.Value
string 2 updated!
$var3[1]
string 2
Run Code Online (Sandbox Code Playgroud)
我希望$var3[1]返回与$var2.Value. 我究竟做错了什么?
可能重复:
C#字符串引用类型?
说,我有一个叫做的字符串
string sample = "Initial value";
Run Code Online (Sandbox Code Playgroud)
传递给方法测试后()
public static void Test(string testString)
{
testString = "Modified Value";
}
Run Code Online (Sandbox Code Playgroud)
如果我在通过测试(样品)后打印'样品',我除了它应该打印"修改值".
但它的印刷"初始价值".如果字符串是引用类型,为什么会这样?
但同样(预期的逻辑),为对象工作.有人可以请我清楚吗?
由于字符串是 dotnet 中的 ref 类型,当我们更新变量 x 时,y 中也应该有更新?(因为它是 x 的参考值)。下面给出的示例程序,当 x 更新时 y 的值如何不改变?
public void assignment()
{
string x= "hello";
string y=x; // referencing x as string is ref type in .net
x = x.Replace('h','j');
Console.WriteLine(x); // gives "jello" output
Console.WriteLine(y); // gives "hello" output
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试为String类创建一个简单的扩展方法,这将允许我提供要附加到包含换行符的现有字符串变量的文本:
string myStr = "Line 1";
myStr.AppendLine("Line 2");
Run Code Online (Sandbox Code Playgroud)
此代码应生成一个打印如下的字符串
Line 1
Line 2
Run Code Online (Sandbox Code Playgroud)
这是我为它写的代码:
public static class StringExtensions
{
public static void appendLine(this String str, string text)
{
str = str + text + Environment.NewLine;
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我调用代码时,新文本永远不会附加到原始实例变量.怎么实现呢?
我有这个方法:
namespace MyProject.String.Utils
{
public static class String
{
public static void formatDecimalSeparator(this string paramString)
{
try
{
if (System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator == ",")
{
paramString = paramString.Replace(".", ",");
}
else
{
paramString = paramString.Replace(",", ".");
}
}
catch
{
throw;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我这样做时:
string myString = "1.23";
myString.formatDecimalSeparator();
Run Code Online (Sandbox Code Playgroud)
结果不是"1,23".变量未更新.所以我必须更改方法以返回一个字符串并将返回值赋给同一个变量.
为什么变量未在呼叫站点更新?扩展方法获取变量的值paramString,我可以在方法中更改它,但在主代码中变量不会更改?
我无法理解这段代码,这里对DataSet对象和字符串变量实现相同,但输出不同,.I可以看到字符串变量输出背后的逻辑,但是对于DataSet,我无法理解为什么!
class Program
{
static void Main(string[] args)
{
DataSet ds = new DataSet();
FillDS(ds);
PrintDS(ds);
string name = "old";
AssignString(name);
PrintString(name);
}
private static void AssignString(string name)
{
name = "new name";
}
private static void PrintString(string name)
{
Console.WriteLine(name);
}
private static void FillDS(DataSet ds)
{
ds.Tables.Add(new DataTable("tbl1"));
ds.Tables.Add(new DataTable("tbl2"));
}
private static void PrintDS(DataSet ds)
{
foreach (DataTable item in ds.Tables)
{
Console.WriteLine(item.TableName);
}
}
}
//Output:
//tbl1
//tbl2
//old
Run Code Online (Sandbox Code Playgroud) 对于项目,我需要将受影响的字符串更改为null或将空白更改为默认值。在我的脑海中,这段代码很有意义,但是我缺少什么呢?它只是返回一个空白,就像它根本没有改变一样。我是编程新手,正在寻求帮助。谢谢 :)。
static void Main(string[] args)
{
string s = "";
ValidateString(s);
Console.WriteLine(s);
}
static string ValidateString(string s)
{
if (s == null || String.IsNullOrWhiteSpace(s))
s = "défault";
return s;
}
Run Code Online (Sandbox Code Playgroud) 在下面的代码中,cpy值的更改不会影响ori值,因此string的行为不像引用类型:
string ori = "text 1";
string cpy = ori;
cpy = "text 2";
Console.WriteLine("{0}", ori);
Run Code Online (Sandbox Code Playgroud)
但是,一个类有不同的行为:
class WebPage
{
public string Text;
}
// Now look at reference type behaviour
WebPage originalWebPage = new WebPage();
originalWebPage.Text = "Original web text";
// Copy just the URL
WebPage copyOfWebPage = originalWebPage;
// Change the page via the new copy of the URL
copyOfWebPage.Text = "Changed web text";
// Write out the contents of the page
// Output=Changed …Run Code Online (Sandbox Code Playgroud)