我能添加List<string>在List<List<string>>数组中这样说:
List<string> first = new List<string> { "one", "two", "three" };
List<string> second = new List<string> { "four", "five", "six" };
List<List<string>> list_array = new List<List<string>> { first, second };
Run Code Online (Sandbox Code Playgroud)
现在我需要创建几个填充了数据库记录的列表,然后将这个列表添加到List<List<string>>数组中:
List<List<string>> array_list;
while (dr.Read())
{
string one = dr["Row1"].ToString();
string two = dr["Row2"].ToString();
List<string> temp_list = new List<string> { one, two };
//Here I need to add temp_list to array_list
}
Run Code Online (Sandbox Code Playgroud) 来自C#中的基本布尔逻辑,我想知道为什么:
Dim b As Boolean
Dim obj As Object = Nothing
'followig evaluates to False'
b = DirectCast(Nothing, Boolean)
'This throws an "Object reference not set to an instance of an object"-Exception'
b = DirectCast(obj, Boolean)
Run Code Online (Sandbox Code Playgroud)
A CType(obj, Boolean)会评估False(就像CBool(obj)).我认为这是因为编译器使用辅助函数,但这不是我的主题.
为什么铸造Nothing到Boolean的计算结果为False,而铸造的对象,是Nothing要Boolean抛出一个Nullreference的异常?那有意义吗?
[Option Strict ON]
Run Code Online (Sandbox Code Playgroud) 我只是想知道什么是替换必须随后替换的字符串字符的最简单方法.
例如:
var str = "[Hello World]";
//enclose all occurences of [ and ] with brackets[]
str = str.Replace("[","[[]").Replace("]","[]]");
Run Code Online (Sandbox Code Playgroud)
[[]Hello World[]][[[]]Hello World[]]原因显然是已经修改过的字符串的第二个替换.
那么如何用包含"坏"字符的字符替换所有"坏"字符的出现?
快速测量所有方法表明StringBuilder是最有效的方法.
190kb文件(全部以毫秒为单位)
Run Code Online (Sandbox Code Playgroud)regexTime 40.5065 replaceTime 20.8891 stringBuilderTime 6.9776
7MB文件
Run Code Online (Sandbox Code Playgroud)regexTime 1209.3529 replaceTime 403.3985 stringBuilderTime 175.2583
顺便说一句,John的直接StringBuilder方法 是Sehe的Aggregate方法的两倍.
我做了一个扩展:
public static String EncloseChars(this string input, char[] charsToEnclose, String leftSide, String rightSide) {
if (charsToEnclose == null || leftSide == null || rightSide …Run Code Online (Sandbox Code Playgroud) 我有以下代码:
public class Character
{
public Vector2 WorldPixelPosition
{
get { return Movement.Position; }
}
public Vector2 WorldPosition
{
get { return new Vector2(Movement.Position.X / Tile.Width, Movement.Position.Y / Tile.Height); }
}
public Vector2 LevelPosition
{
get { return new Vector2(WorldPosition.X % Level.Width, WorldPosition.Y % Level.Height); }
}
}
Run Code Online (Sandbox Code Playgroud)
现在在我的代码中的其他地方,我在循环中对Character.LevelPosition进行了大约2500次调用.这意味着每个更新周期,5000'新'Vector2正在制作,而在我的笔记本电脑上,它确实降低了帧速率.
我通过创建暂时修复了它
var levelPosition = Character.LevelPosition;
Run Code Online (Sandbox Code Playgroud)
在我启动循环之前,但是每次遇到类似的情况时,我都会感觉到它的丑陋代码.也许它是 - 要走的路,但我想确定一下.
有没有更好或普遍接受的方式来做到这一点?
我正在使用XNA-Framework,它使用Vector2的是.
有没有办法找出"TextChanged"事件是否被触发,因为
- 用户正在输入文本框或
- 程序员调用myTextBox.Text ="something"?
只是为了给你一些颜色,当用户在文本框中键入每个字母时我不想做出反应,因此我使用"Validated"事件来捕获用户完成后我可以做出反应.问题是,当程序员执行"myTextbox.Text ="某事时,我没办法抓住.我知道捕获更改的唯一方法是使用TextChanged,但后来我不想做出反应用户在文本框中键入每个字母.有任何建议吗?
考虑我有一些抽象的 Vehicle类和汽车,卡车,摩托车抽象类派生自Vehicle.还想象一下,我必须能够为卡车和摩托车制造基于燃料的汽车或电动汽车等.(具体课程)
两个问题:
1.考虑到我想以多态的方式在车辆中充满能量而不知道它是什么.例如,如果车辆是基于燃料的,我想用燃料填充它,方法应该有3个参数:
void FillUpEnergy(EfuelType i_fuelType,int amounOfEnergy, int maxAmountOfEnergy)
但对于电动车辆,我需要几乎相同的功能,但这次当然没有燃料类型,例如(2个参数):
void FillUpEnergy(int amounOfEnergy, int maxAmountOfEnergy)
Run Code Online (Sandbox Code Playgroud)
我可以FillUpEnergy用上述约束做一个多态方法吗?(不同方法的签名)
2.在我的实现中,所有具体类都有Engine(另一个抽象类)的引用,它代表一个FuelEngine或ElectricEngine(我从Engine派生的其他具体类).例如,我有一个名为的具体类ElectricCar,它包含一个引用ElectricEngine.
这种架构是否足够好或是否有更好的方法来实施车库系统?(在面向对象设计等方面......)
我正在开发一个可以在世界上许多国家看到的应用程序.没有多少国家/地区显示小时,分钟和秒以及其他内容:作为分隔符,但有一些,我想确保为其所在地区正确格式化时间.DateTime很棒,但TimeSpan不是.这些片段来自我在Visual Studio 2010中的即时窗口,使用.Net 4,我的区域设置为Malayalam(印度).dateTime.Now调用还反映了我的时钟,Microsoft Outlook和其他领域的时间显示方式.
DateTime.Now.ToString()
"02-10-12 17.00.58"
Run Code Online (Sandbox Code Playgroud)
http://msdn.microsoft.com/en-us/library/dd784379.aspx说"如果formatProvider为null,则使用与当前区域性关联的DateTimeFormatInfo对象.如果format是自定义格式字符串,则为formatProvider参数被忽略了." 按理说那我甚至不需要传递当前的CultureInfo.我想要的格式是hh.mm.ss,但是在大多数其他语言中显而易见hh:mm:ss,如果还有其他可能性,它应该自动反映这些 - 基本上TimeSpan 应该是文化意识,就像DateTime一样是.
然而:
timeRemaining.ToString()
"00:02:09"
timeRemaining.ToString("c")
"00:02:09"
timeRemaining.ToString("c", CultureInfo.CurrentCulture)
"00:02:09"
timeRemaining.ToString("g")
"0:02:09"
timeRemaining.ToString("G")
"0:00:02:09.0000000"
timeRemaining.ToString("t")
"00:02:09"
timeRemaining.ToString("g", CultureInfo.CurrentCulture)
"0:02:09"
timeRemaining.ToString("g", CultureInfo.CurrentUICulture)
"0:02:09"
timeRemaining.ToString("G", CultureInfo.CurrentUICulture)
"0:00:02:09.0000000"
timeRemaining.ToString("G", CultureInfo.CurrentCulture)
"0:00:02:09.0000000"
timeRemaining.ToString("t", CultureInfo.CurrentCulture)
"00:02:09"
Run Code Online (Sandbox Code Playgroud)
我正在寻找一个简单的单行来以文化意识的方式输出timeSpan.任何想法都表示赞赏.
这是一个例子:
public class FotoLiveLove
{
public string Tipologia { get; set; }
public string URL { get; set; }
}
IList<FotoLiveLove> fotoLiveLove = xDoc["statuses"].Select(x => new
{
Tipologia = "twitter",
URL = (string)x["URLJSON"]
}).ToList();
Run Code Online (Sandbox Code Playgroud)
但它表示无法将匿名类型#1转换为FotoLiveLove.
我试过这个解决方案就像.
select dateadd(wk, datediff(wk, 0, getdate()), 0)as StartDate ,
(select dateadd(wk, datediff(wk, 0, getdate()), 0) + 5) as EndDate
Run Code Online (Sandbox Code Playgroud)
它给出了星期一 - 星期六的结果,但是星期天它给了我下周的日子
我希望周日作为周的最后一天,周一作为周的第一天..
请帮忙...
我有一个简单的c#应用程序,你必须在其中输入数据DataGridView.我已经为列实现了一些验证器,如空值或非数字输入.按下按钮后,我会进行检查foreach (DataGridViewRow row in dataGridView1.Rows) {...}
我面临的问题是它也试图验证最后一行DataGridView,尽管这一行是自动添加的并且是空的.所以我在这里陷入困境......
private void button1_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
string inputItemNr;
string inputMHD;
string inputCharge;
string inputSupplNr;
string inputPrnCnt;
UInt32 itemnr;
DateTime mhd;
string mhdFormat = "yyMMdd";
string batch;
byte prncnt;
if (row.Cells[0].Value == null)
{
MessageBox.Show("Enter item number");
return;
}
else
{
inputItemNr = row.Cells[0].Value.ToString();
}
if (!UInt32.TryParse(inputItemNr, out itemnr))
{
MessageBox.Show("Incorrect item number: " + inputItemNr);
return;
}
if (row.Cells[1].Value == null) …Run Code Online (Sandbox Code Playgroud) c# ×8
.net ×3
vb.net ×2
.net-3.5 ×1
arrays ×1
c#-4.0 ×1
casting ×1
cultureinfo ×1
datagridview ×1
datetime ×1
getter ×1
inheritance ×1
linq ×1
list ×1
new-operator ×1
oop ×1
performance ×1
polymorphism ×1
replace ×1
sql-server ×1
string ×1
winforms ×1
xna ×1