例如,如果我有,Object.ObjectTwo.Property
并且我不想通过一直写下来使我的代码混乱,是否有办法缩短它?
Object.ObjectTwo.Property = something
我希望能够写作,而不是写作myVariable = something
.
我尝试搜索时找不到任何东西.
编辑:有问题的成员是一个属性.
我有以下示例代码(仅用于C#3.5学习目的!).
我正在调用接受IEnumerable和sort函数的Sort函数.如果我使用lambda表达式调用它(案例A),编译器可以派生返回类型TResult,但是当我传递func SortInt(案例B)时,编译器会抛出错误!
我无法理解为什么编译器在第二种情况下无法派生TResult!我似乎传递了完全相同的信息.或者这不准确?
请帮忙 !
int[] intArray = { 1, 3, 2, 5, 1 };
IEnumerable<int> intArray2 = Sort(intArray, x => SortInt(x)); // <= CASE A - OK !
IEnumerable<int> nextIntArray = Sort(intArray, SortInt); // <= CASE B - Compile Error: Cannot Infer Type !
public static IEnumerable<TResult> Sort<T, TResult>(IEnumerable<T> toBeSorted,
Func<IEnumerable<T>, IEnumerable<TResult>> sortFunc)
{
return sortFunc(toBeSorted);
}
public static IEnumerable<int> SortInt(IEnumerable<int> array)
{
return array.OrderByDescending(x => x);
}
Run Code Online (Sandbox Code Playgroud) 我编写了ASP.NET页面来管理表单.它们基于以下基类.
public abstract class FormPageBase<TInterface, TModel> : Page, IKeywordProvider
where TModel:ActiveRecordBase<MasterForm>, TInterface, new()
where TInterface:IMasterForm
{
public TInterface FormData { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
一个示例SubClass在这里:
public partial class PersonalDataFormPage : FormPageBase<IPersonalDataForm, PersonalDataForm>, IHasFormData<IPersonalDataForm>, IHasContact
{
}
Run Code Online (Sandbox Code Playgroud)
下面我在页面上有一个usercontrol,我想从页面"使用""FormData",以便它可以读/写它.
然后,我有一个更"通用"的用户控件,我想在我所有的表单子类的基接口上操作... IMasterForm
但是当usercontrol尝试强制转换Page.FormData时(尝试将页面强制转换为IHasFormData<IMasterForm>
它)告诉我该页面IHasFormData<IFormSubclass>
即使我对IFormSubclass有一个约束,它说它也是IMasterForm
无论如何,我可以从通用子类转换为通用超类,还是这个"协方差"和C#4.0的东西?
public abstract class FormControlBase<T> : UserControl, IKeywordProvider
where T:IMasterForm
{
protected T FormData { get; set; }
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
//This cast is failing when my common control's T does not …
Run Code Online (Sandbox Code Playgroud) 昨天我发布了一个关于new/virtual/override关键字的问题,我从你的答案中学到了很多东西.但我仍有一些疑虑.
在所有"盒子"之间,我与类型的方法表中真正发生的事情失去联系.例如:
interface I1 { void Draw(); }
interface I2 { void Draw(); }
class A : I1, I2
{
public void Minstance() { Console.WriteLine("A::MInstance"); }
public virtual void Draw() { Console.WriteLine("A::Draw"); }
void I2.Draw() { Console.WriteLine("A::I2.Draw"); }
}
class B : A, I1, I2
{
public new virtual void Draw() { Console.WriteLine("B::Draw"); }
void I1.Draw() { Console.WriteLine("B::I1.Draw"); }
}
class Test
{
public static void Main()
{
A a = new B();
a.Draw();
I1 i1 = new A();
i1.Draw(); …
Run Code Online (Sandbox Code Playgroud) 我正在尝试计算浮动数组的平均值。我需要使用索引,因为它在二进制搜索中,因此顶部和底部将移动。(大图,我们正在尝试优化半程估计,因此我们不必在每次通过时都重新创建阵列)。
无论如何,我编写了一个自定义的平均循环,与c#Average()方法相比,我得到的精度低2位
float test = input.Average();
int count = (top - bottom) + 1;//number of elements in this iteration
int pos = bottom;
float average = 0f;//working average
while (pos <= top)
{
average += input[pos];
pos++;
}
average = average / count;
Run Code Online (Sandbox Code Playgroud)
例:
0.0371166766-C# 0.03711666-我的循环 125090.148-C# 125090.281-我的循环
我不会给这项工作:
public interface ICallBack
{
void Handle<T>(T arg);
}
public class MessageHandler : ICallBack
{
public void Handle<T>(T arg)
{
string name = typeof(T).Name;
Console.WriteLine(name);
}
public void Handle(int arg)
{
string name = "wow, an int";
Console.WriteLine(name);
}
}
public class Worker
{
public void DoSomething(ICallBack cb)
{
cb.Handle(55);
}
}
//...
Worker foo = new Worker();
ICallBack boo = new MessageHandler();
//I want this to print "Wow, an int"
foo.DoSomething(boo)
Run Code Online (Sandbox Code Playgroud)
不幸的是,它调用通用入口点而不是"专用"入口点.好吧,那就是你的接口.
我也尝试过相同的方法,但用特定的通用签名替换特定于int的签名Mojo
:
public void Handle<T>(T arg) …
Run Code Online (Sandbox Code Playgroud) 我正在生成随机数序列.序列仅包括0和1.我将每个序列放在一个单独的文本文件中,然后我尝试归档文件(格式为.zip).我正在使用System.Random生成每个序列的元素.初看起来,序列似乎确实是随机的.
奇怪的是,无论生成的.txt文件的大小是多少,压缩的.zip文件的大小总是等于.txt文件大小的相同比例~17%.
但从理论上讲,对于一个非常随机的序列,压缩的.zip文件的大小应该与.txt文件的大小基本相同 - 也就是说,应该几乎没有压缩.否则,序列至少是部分可预测的(在这种"翻转硬币"式实验中这是不可能的).
所以这意味着我的"归档器"知道如何识别序列是由System.Random中实现的特定伪随机生成器生成的.
这里我有两个问题:
如何生成归档器无法压缩的伪随机序列?也许有一些已知的技巧?
为什么17%的比率如此稳定并且不依赖于序列的长度(即,.txt文件的大小).
谢谢你的帮助!
假设我有这段代码:
public static class Converters {
public static Dictionary<Unit, Dictionary<string, Func<float, float>>> ConverterDictionary =
new Dictionary<Unit, Dictionary<string, Func<float, float>>>
{
{
Unit.MS, new Dictionary<string, Func<float, float>>() {
{"m/s -> km/h", MStoKMH },
{"m/s -> mph", MStoMPH }
}
}
};
private static Func<float, float> MStoKMH = val => val * 3.6f;
private static Func<float, float> MStoMPH = val => val * 2.23693629f;
}
public enum Unit {
MS
}
Run Code Online (Sandbox Code Playgroud)
我在其他地方尝试使用以下代码MStoKMH
从ConverterDictionary
(并调用它)中检索函数:
Func<float, float> test = Converters.ConverterDictionary[Unit.MS]["m/s -> …
Run Code Online (Sandbox Code Playgroud) 将对象数组转换为整数数组的最简单方法是什么?
ArrayList al = new ArrayList();
object arrayObject = al.ToArray();
int[]arrayInteger = ?
Run Code Online (Sandbox Code Playgroud)
谢谢
您如何看待以下断言?
Assert.IsTrue(condition1 && condition2);
Run Code Online (Sandbox Code Playgroud)
我在审查会议期间想出了这种类型的断言.据我所知,在断言中使用&&运算符不是一个好习惯.我想知道其他人的想法?值得一提的是,这是下一次审查会议期间的问题吗?或者这仅仅是个人偏好?
c# ×9
.net ×3
.net-3.5 ×2
generics ×2
average ×1
compression ×1
covariance ×1
dictionary ×1
interface ×1
lambda ×1
new-operator ×1
null ×1
nunit ×1
overriding ×1
random ×1
tdd ×1
unit-testing ×1
virtual ×1
zip ×1