我想从列表中获取值的总和.
例如:我在列表中有4个值1 2 3 4我想对这些值求和并在Label中显示它
码:
protected void btnCalculate_Click(object sender, EventArgs e)
{
string monday;
TextBox txtMonTot;
List<string> monTotal = new List<string>();
if (Application["mondayValues"] != null)
{
List<string> monValues = Application["mondayValues"] as List<string>;
for (int i = 0; i <= gridActivity.Rows.Count - 1; i++)
{
GridViewRow row = gridActivity.Rows[i];
txtMonTot = (TextBox)row.FindControl("txtMon");
monday = monValues[i];
monTotal.Add(monday);
}
}
}
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?提前致谢
是否有快速内置方法来检查是否IEnumerable<string>只包含不同的字符串?
一开始我开始:
var enumAsArray = enum.ToArray();
if (enumAsArray.Length != enumAsArray.Distinct().Count())
throw ...
Run Code Online (Sandbox Code Playgroud)
但是,这看起来像是O(2n) - 是吗?ToArray()可能是O(1)?
这看起来更快:
var set = new HashSet<string>();
foreach (var str in enum)
{
if (!set.Add(str))
throw ...
}
Run Code Online (Sandbox Code Playgroud)
这应该是O(n),但是,是否也有内置方式?
编辑:也许Distinct()在内部使用它?
解决方案: 在考虑了所有的评论和答案后,我为我的第二个解决方案编写了一个扩展方法,因为这似乎是最快的版本,也是最具可读性的:
public static bool ContainsDuplicates<T>(this IEnumerable<T> e)
{
var set = new HashSet<T>();
// ReSharper disable LoopCanBeConvertedToQuery
foreach (var item in e)
// ReSharper restore LoopCanBeConvertedToQuery
{
if (!set.Add(item))
return true;
}
return false;
}
Run Code Online (Sandbox Code Playgroud) // x is compiled as an int
var x = 10;
// y is compiled as a string
var y = "Hello";
// z is compiled as int[]
var z = new[] { 0, 1, 2 };
Run Code Online (Sandbox Code Playgroud)
但
// ano is compiled as an anonymous type
var ano = new { x1 = 10, y1 = "Hello" };
Run Code Online (Sandbox Code Playgroud)
ano创建的对象属性是只读的.我想弄清楚为什么这些属性是只读的.建议表示赞赏?
编辑:
var ano1 = new { x1 = 10, y1 = "Hello" };
var ano2 = new { x1 = …Run Code Online (Sandbox Code Playgroud) 我正在学习C#而且我对它很新,请原谅我这个看似愚蠢的问题.我有一些Java经验,我注意到C#程序main()在主类中也需要一个方法.
如果我想创建一个不是主类的类,即我导入主类的类,该怎么办?
我试图这样做,当我编译(通过cmd使用csc File.cs)时,编译器说它将生成的.exe没有main()方法.这是否意味着我错了,每个班级都需要一个main()方法,或者我错误地编译它?
也许是代码中的问题(因为我依赖于我对Java语法的了解),看起来像这样:
public class Class
{
int stuff;
public Class(int stuff)
{
this.stuff = stuff;
stuff();
}
public void method()
{
stuff();
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:我担心这是非常误解的.我不是在问这个文件是否需要一个main方法,我问我怎样才能把这个类导入到另一个类中,因为我意识到如果我要这样做我就不能有一个main(就像我说的那样,我有一些Java经验),但每当我尝试编译没有一个编译器告诉我,我需要一个.
使用以下代码启动新的控制台应用程序 -
class Program
{
static void Main(string[] args)
{
while (true)
{
Task<string> readLineTask = Console.In.ReadLineAsync();
Debug.WriteLine("hi");
}
}
}
Run Code Online (Sandbox Code Playgroud)
Console.In.ReadLineAsync是阻塞的,直到在控制台中输入一行才返回..所以"Hi"永远不会写入控制台.
在Console.In.ReadLineAsync上使用await也会阻塞.
据我所知,新的Async CTP方法不会阻塞.
这是什么原因?
这是另一个例子
static void Main(string[] args)
{
Task delayTask = Task.Delay(50000);
Debug.WriteLine("hi");
}
Run Code Online (Sandbox Code Playgroud)
这表现如我所料,它直接到下一行并打印"hi",因为Task.Delay不会阻塞.
请考虑以下代码:
using System;
namespace memoryEater
{
internal class Program
{
private static void Main(string[] args)
{
Console.WriteLine("alloc 1");
var big1 = new BigObject();
Console.WriteLine("alloc 2");
var big2 = new BigObject();
Console.WriteLine("null 1");
big1 = null;
//GC.Collect();
Console.WriteLine("alloc3");
big1 = new BigObject();
Console.WriteLine("done");
Console.Read();
}
}
public class BigObject
{
private const uint OneMeg = 1024 * 1024;
private static int _idCnt;
private readonly int _myId;
private byte[][] _bigArray;
public BigObject()
{
_myId = _idCnt++;
Console.WriteLine("BigObject {0} creating... ", _myId);
_bigArray …Run Code Online (Sandbox Code Playgroud) 我想知道将一个异常从一个方法传递给我的表单的正确方法是什么.
public void test()
{
try
{
int num = int.Parse("gagw");
}
catch (Exception)
{
throw;
}
}
Run Code Online (Sandbox Code Playgroud)
形成:
try
{
test();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
Run Code Online (Sandbox Code Playgroud)
这样我就看不到我的文本框了.
首先,我试图找出使用Interlocked仍然需要volatile字段定义,这是我真正的问题.
但.懒得分析生成的MSIL我决定在实践中检查它.
我想为MSDN例如volatile使用时的代码应该在发布版本与优化打破.没有什么打破.代码工作正常(在这种情况下 - 主线程正常终止)开启和关闭优化.
volatile当我从一个线程使用Interlocked并从另一个线程读取而没有锁定时,我是否仍然需要字段上的关键字?volatile有区别?volatile关键字并在发布中构建时,为什么MSDN示例仍然有效?用于说明问题1的代码段.
class Example
{
volatile int val;
void Do()
{
Task.Run(() => { while (val == 0) Console.WriteLine("running"); });
Thread.Sleep(1000);
Interlocked.Increment(ref val);
Console.WriteLine("done.");
Console.ReadLine();
}
}
Run Code Online (Sandbox Code Playgroud) 我看到F#使用**运算符来表示幂,所以2 ** 5 = 32.这与C#不同,您可以选择在自定义类型中使用"^"运算符,但由于某些原因,内置数字类型不使用它.
但是如何在C#中实现**运算符以用于F#项目?
如果我在C#中这样做:
public static Integer operator ^(Integer left, Integer right)
{
if (Integer.IsNaN(left) || Integer.IsNaN(right)) return NaN;
return left.RaiseToPower(right);
}
Run Code Online (Sandbox Code Playgroud)
它编译得很好,我可以像'+'运算符一样使用它,但这些都不适用于F#:
let intgr3 = intgr1 ** intgr2
let intgr3 = intgr1 ^ intgr2
Run Code Online (Sandbox Code Playgroud)
在C#中,这不起作用:
public static Integer operator **(Integer left, Integer right)
{
if (Integer.IsNaN(left) || Integer.IsNaN(right)) return NaN;
return left.RaiseToPower(right);
}
Run Code Online (Sandbox Code Playgroud)
那么如何**在C#中定义运算符的F#等价物呢?
谢谢.
我有一个实体,需要在其上跟踪员工工资率的历史记录.employeeRate对象是:
public class EmployeeRate : BaseEntity
{
[Key]
public int EmployeeRateId { get; set; }
public int EmployeeId { get; set; }
public double Rate { get; set; }
}
public class BaseEntity
{
public bool ActiveFlag { get; set; }
public DateTime CreateStamp { get; set; }
public DateTime UpdateStamp { get; set; }
public string LastModifiedBy { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
因此,为了跟踪历史记录,当我"更新"员工费率时,我使用其ID检索所有旧记录并将ActiveFlag设置为false,然后添加具有新工资率且ActiveFlag设置为true的新实体.这是我的这个操作的功能:
private EmployeeRate UpdateRate(EmployeeRate model, string currentUser)
{
var existingRates = _context.EmployeeRates.Where(r => r.EmployeeId == model.EmployeeId).ToList();
foreach(var …Run Code Online (Sandbox Code Playgroud) c# ×10
asp.net ×1
asp.net-core ×1
async-await ×1
class ×1
collections ×1
exception ×1
exponent ×1
f# ×1
finalizer ×1
var ×1
volatile ×1
winforms ×1