我已经安装了一周前发布的Visual Studio 2017社区,我开始探索C#7的新功能.
所以我创建了一个返回两个值的简单方法:
public class Program
{
public static void Main(string[] args)
{
(int sum, int count) a = ReturnTwoValues();
}
static (int sum, int count) ReturnTwoValues() => (1, 1);
}
Run Code Online (Sandbox Code Playgroud)
编译器生成错误:
错误CS8137无法定义使用元组的类或成员,因为无法找到编译器所需类型"System.Runtime.CompilerServices.TupleElementNamesAttribute".你错过了参考吗?
我试着用这个名字在框架中找到一个引用,但没有运气!
如果我们需要额外的东西来使用C#7.0功能,那么我们需要为每个项目做到这一点非常奇怪吗?
我玩这个游戏是Task.ConfigureAwait
为了更好地理解引擎盖之外的内容。所以我在将一些 UI 访问内容与ConfigureAwait
.
下面是使用简单 Windows 窗体的示例应用程序,1Button
后面是测试结果:
private async void btnDoWork_Click(object sender, EventArgs e)
{
List<int> Results = await SomeLongRunningMethodAsync().ConfigureAwait(false);
int retry = 0;
while(retry < RETRY_COUNT)
{
try
{
// commented on test #1 & #3 and not in test #2
//if(retry == 0)
//throw new InvalidOperationException("Manually thrown Exception");
btnDoWork.Text = "Async Work Done";
Logger.Log("Control Text Changed", logDestination);
return;
}
catch(InvalidOperationException ex)
{
Logger.Log(ex.Message, logDestination);
}
retry++;
}
}
Run Code Online (Sandbox Code Playgroud)
现在点击按钮后:
测试1 日志结果:( 与上面的代码完全相同)
1. …
Run Code Online (Sandbox Code Playgroud) 我正在阅读此页面,该页面在Visual Studio 17 RC 的发行说明中正式引用,它声明如下:
出于重载覆盖隐藏的目的,相同类型和长度的元组以及它们的基础ValueTuple类型被认为是等效的.所有其他差异都是无关紧要的.
覆盖成员时,允许使用具有与基本成员相同或不同字段名称的元组类型.
在基本成员签名和派生成员签名之间使用相同字段名称作为非匹配字段的情况,编译器会报告警告
给出一个镜头:
public abstract class Foo
{
public abstract void AbstractTuple((int a, string b) tuple);
public virtual void OverrideTuple((int a, string b) tuple)
{
Console.WriteLine($"First= {tuple.a} Second = {tuple.b}");
}
}
public class Bar : Foo
{
public override void AbstractTuple((int c, string d) tuple)
{
Console.WriteLine($"First= {tuple.c} Second= {tuple.d}");
}
public override void OverrideTuple((int c, string d) tuple)
{
base.OverrideTuple(tuple);
}
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
错误CS8139'Bar.AbstractTuple((int c,string d))':重写继承的成员'Foo.AbstractTuple((int …
我有一个 cmbPlace(组合框),它的项目自动填充了 System.IO 驱动器(C:\、D:\ 等)。同时它也有验证事件。代码如下:
using System.IO;
public FNamefile()
{
InitializeComponent();
DriveInfo[] allDrives = DriveInfo.GetDrives();
foreach (DriveInfo d in allDrives)
{
cmbPlace.Items.Add(d.Name);
}
}
private void FNamefile_Load(object sender, EventArgs e)
{
errorProvider1.ContainerControl = this;
}
private bool ValidatePlace()
{
bool bStatus = true;
int m = cmbPlace.SelectedIndex;
if ((cmbPlace.Items[m]).ToString() == cmbPlace.Text)
{
errorProvider1.SetError(cmbPlace, "");
}
else if (cmbPlace.Text == "" || (cmbPlace.Items[m]).ToString() != cmbPlace.Text)
{
errorProvider1.SetError(cmbPlace, "Please enter a valid location");
bStatus = false;
}
return bStatus;
}
private void …
Run Code Online (Sandbox Code Playgroud) 如何在1个语句中解析2个double值而不是2个if语句?
我的代码:
double a, b;
while (true)
{
if (Double.TryParse(Console.ReadLine(), out a))
{
}
else
{
continue;
}
if (Double.TryParse(Console.ReadLine(), out b))
{
}
else
{
continue;
}
break;
}
Run Code Online (Sandbox Code Playgroud)
我已经搜索过了,但没有找到任何好结果
我需要在SQL中获取目标值(需要从0自动递增10,从0开始)以及我已经在查询中获取的结果.
基本上它需要是一个逻辑:
set @target =0
Run Code Online (Sandbox Code Playgroud)
并在我的显示查询中:
display -> @target = @target +10
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助.
我想将一个事件添加到以编程方式生成的按钮,如下所示:
Button activityButton = new Button();
activityButton.Click += new EventHandler(onChangeActivityFilter);
Run Code Online (Sandbox Code Playgroud)
我在第二行得到以下异常:
不能隐式转换
System.EventHandler
到System.Windows.RoutedEventhandler
onChangeActivityFilter方法如下所示:
private void onChangeActivityFilter(object sender, EventArgs e)
{
}
Run Code Online (Sandbox Code Playgroud)
我想知道我做错了什么.
在最小的奇怪,但我相信存在一个解释...我有一个接口(IRepository)有6个方法由类实现.但是当我把代码用于实现接口时,VS2015并没有向我显示实现类的选项,如果我没有手动实现并编译项目它不会显示编译错误,不应该吗?如果它出现编译错误告诉我,我没有实现接口.
接口:
public interface IRepository<T> where T : class
{
IQueryable<T> GetAll ();
IQueryable<T> FindBy ( Expression<Func<T, bool>> predicate );
void Add ( T entity );
void Delete ( T entity );
void Edit ( T entity );
void Save ();
}
Run Code Online (Sandbox Code Playgroud)
应该实现IRepository但不实现的类,它不会抛出编译错误:
public class GenericRepository<T> where T : class, IRepository<T>
{
}
Run Code Online (Sandbox Code Playgroud) 我有以下NotificationViewModel
设计:
public class NotificationViewModel
{
public string NotificationText { get; set; }
public DateTime Moment { get; set; }
public string Icon { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我正在生成我的通知数据,如下所示:
List<NotificationViewModel> model = new List<NotificationViewModel>();
int days = DateTime.Now.DayOfWeek - DayOfWeek.Sunday;
DateTime weekStart = DateTime.Now.AddDays(-days);
DateTime weekEnd = weekStart.AddDays(6);
var purchases = await context.tbl1.Where(x => x.created_date <= weekEnd && x.created_date >= weekStart)
.Select(x => new NotificationViewModel()
{
Icon = "fa fa-gbp",
Moment = x.created_date,
NotificationText = "A new purchase.",
}).ToListAsync(); …
Run Code Online (Sandbox Code Playgroud) c# ×9
c#-7.0 ×2
winforms ×2
.net ×1
asp.net ×1
asp.net-mvc ×1
asynchronous ×1
if-statement ×1
int ×1
interface ×1
linq ×1
object ×1
overriding ×1
sql ×1
sql-server ×1
validation ×1
wpf ×1