在C#中,我想用空字符串初始化字符串值.
我该怎么做?什么是正确的方法,为什么?
string willi = string.Empty;
Run Code Online (Sandbox Code Playgroud)
要么
string willi = String.Empty;
Run Code Online (Sandbox Code Playgroud)
要么
string willi = "";
Run Code Online (Sandbox Code Playgroud)
或者是什么?
这是我的GeneralFunctions:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
/// <summary>
/// Summary description for GeneralFunctions
/// </summary>
public class GeneralFunctions
{
public GeneralFunctions ()
{
//
// TODO: Add constructor logic here
//
}
public static DataTable GetData ( string query )
{
SqlDataAdapter dataAdapter;
DataTable table;
try
{
dataAdapter = new SqlDataAdapter( query, GetConnectionString() );
table = new DataTable();
dataAdapter.Fill( table );
return table;
}
catch ( Exception ex )
{
} …
Run Code Online (Sandbox Code Playgroud) 我了解分配值与不分配值的区别,我想了解的是分配如何在内存中处理。
什么将存储在HEAP和堆栈中?哪一个是最有效的?
例如,使方法签名更有效
private Item GetItem(pageModel page, string clickableText = null);
Run Code Online (Sandbox Code Playgroud)
要么
private Item GetItem(pageModel page, string clickableText = "");
Run Code Online (Sandbox Code Playgroud)
注意:
问题不在于使用哪个。这是关于它们在内存上的差异。提议的方法可能被调用数百次-因此,不同的变量分配可能/会产生影响吗?
这是我的代码:
private void startButton_Click(object sender, EventArgs e)
{
DriveInfo[] drives = DriveInfo.GetDrives();
string drivenames;
for (int i = 0; i < drives.Count(); i++)
drivenames = drives[i].Name;
MessageBox.Show(drivenames); // --> For debug purposes only
strCmdText = "del /f /s " + drivenames + "*.sfk";
System.Diagnostics.Process.Start("CMD.exe", strCmdText);
}
Run Code Online (Sandbox Code Playgroud)
我试图将计算机上的驱动器号写入字符串,但是,当我尝试使用该字符串时,它表示使用未分配的局部变量'drivenames'.这有什么问题?