我正在玩一个非常简单的程序来获取一系列双打并返回标准偏差.这部分工作但我想让代码更可重用.我想这样做,所以该方法可以接受任何类型的参数,可以被认为是数字并返回标准偏差而不是硬编码双重类型(就像我最初在这个程序中做的那样).如何解决这个问题以及适当的术语是什么?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
double[] avg = { 3.4, 55.6, 10.0, 4.5, 2, 2 };
double x = avg.Average();
//first round of testing
Console.WriteLine("The average of the first array is below ");
Console.WriteLine(x);
Console.WriteLine("below should be the standard deviation!");
Console.WriteLine(CalculateStandardDeviation(avg));
Console.ReadLine();
int[] intAvg = { 4, 3, 5, 6, 2 };
double secondAvg = intAvg.Average();
Console.WriteLine("The average of the second array is below "); …
Run Code Online (Sandbox Code Playgroud) 有没有任何函数,它为同一个字符串提供相同的哈希码?
我在创建2个不同的字符串(但具有相同的内容)时遇到了麻烦,但它们的哈希码是不同的,因此在a中没有正确使用Dictionary
.
我想知道当键是字符串时使用什么GetHashCode()
功能Dictionary
.
我正在这样建造我的:
public override int GetHashCode()
{
String str = "Equip" + Equipment.ToString() + "Destiny" + Destiny.ToString();
return str.GetHashCode();
}
Run Code Online (Sandbox Code Playgroud)
但是它为使用此代码的每个实例产生不同的结果,尽管字符串的内容是相同的.
我发现了IL code
一个简单的程序:
long x = 0;
for(long i = 0;i< int.MaxValue * 2L; i++)
{
x = i;
}
Console.WriteLine(x);
Run Code Online (Sandbox Code Playgroud)
我在发布模式下构建此代码并IL code
生成:
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Code size 28 (0x1c)
.maxstack 2
.locals init ([0] int64 x,
[1] int64 i)
IL_0000: ldc.i4.0
IL_0001: conv.i8
IL_0002: stloc.0
IL_0003: ldc.i4.0
IL_0004: conv.i8
IL_0005: stloc.1
IL_0006: br.s IL_000f
IL_0008: ldloc.1
IL_0009: stloc.0
IL_000a: ldloc.1
IL_000b: ldc.i4.1
IL_000c: conv.i8
IL_000d: …
Run Code Online (Sandbox Code Playgroud) 我使用visualstudio online存储库.我从项目的收集中删除了[ProjectName]项目之后我只在一台计算机上收到错误TF10175:[ProjectName]团队项目不存在.
有什么想法解决这个问题吗?
is运算符仅考虑引用转换,装箱转换和拆箱转换.其他转换(例如用户定义的转化)不予考虑.
这在实践中意味着什么?用它来检查结构是否是某种类型是错误的吗?例如,
public struct Point2D
{
public int X;
public int Y;
...
public override bool Equals(Object value)
{
if (value != null && value is Point2D) // or if (value != null && GetType() == value.GetType())
{
Point2D right = (Point2D)value;
return (X == right.X && Y == right.Y);
}
else return false;
}
...
}
Run Code Online (Sandbox Code Playgroud) 今天我碰到一个两难之间有什么区别来ReadOnlyCollection<T>
,并ReadOnlyCollectionBuilder<T>
在.net中?
在ReadOnlyCollection<T>
对象中,我们无法添加和删除项目.
在ReadOnlyCollectionBuilder<T>
对象中我们可以添加和删除项目.
如果我们可以添加和删除ReadOnlyCollectionBuilder<T>
对象中的项目,那么为什么名称只读?
我有一个类,它创建一个Image
from Byte[]
,以及一些其他逻辑,我正在尝试编写一个单元测试,断言Image
从我的类返回的实例与我Image
在单元测试中的假实例相同.
我找不到一个可靠的方法:
Image
\ Byte[]
\资源\ 东西.Byte[]
代表假的东西传递给我的班级.Image
从我的类返回是一样的我的假的东西.这是我到目前为止提出的代码:
Bitmap fakeBitmap = new Bitmap(1, 1);
Byte[] expectedBytes;
using (var ms = new MemoryStream())
{
fakeBitmap.Save(ms, ImageFormat.Png);
expectedBytes = ms.ToArray();
}
//This is where the call to class goes
Image actualImage;
using (var ms = new MemoryStream(expectedBytes))
actualImage = Image.FromStream(ms);
Byte[] actualBytes;
using (var ms = new MemoryStream())
{
actualImage.Save(ms, ImageFormat.Png);
actualBytes …
Run Code Online (Sandbox Code Playgroud) 是否有可能在新的子进程中运行方法?在我的例子中,我可以执行方法Run,它将在新进程中执行私有方法doAction(而不是线程!)
public class MyClass
{
public void Run()
{
//what should I do there to run 'doAction' in new process?
doAction();
}
private void doAction()
{
...
}
}
Run Code Online (Sandbox Code Playgroud) 如何使用printf/sprintf打印带有实际%符号的文本?例如
let fn = 5
printf "%i%" fn
Run Code Online (Sandbox Code Playgroud)
给出编译错误.显而易见的\%
也不起作用.
我编写了一个CSharpSyntaxRewriter
用于从方法中删除属性的文件,但是当我从方法中删除所有属性时,我正在努力保留属性之前的任何东西(直到前一个方法).
这适用于具有多个属性的方法,但不适用于只有一个属性.
这是一个最小的repro:
void Main()
{
var code = @"namespace P
{
class Program
{
public void NoAttributes() { }
//???
[TestCategory(""Atomic"")]
public void OneAtt1() { }
[TestCategory(""Atomic"")]
public void OneAtt2() { }
[TestMethod, TestCategory(""Atomic"")]
public void TwoAtts() { }
}
}";
var tree = CSharpSyntaxTree.ParseText(code);
var rewriter = new AttributeRemoverRewriter();
var rewrittenRoot = rewriter.Visit(tree.GetRoot());
Console.WriteLine(rewrittenRoot.GetText().ToString());
}
public class AttributeRemoverRewriter : CSharpSyntaxRewriter
{
public override SyntaxNode VisitAttributeList(AttributeListSyntax attributeList)
{
var nodesToRemove =
attributeList
.Attributes
.Where(att => (att.Name as …
Run Code Online (Sandbox Code Playgroud)