我正在尝试在我的应用程序中实现任务.
这是示例代码:
有一个简单的接口我,3个类派生它(A,B,C)我创建一个Is的列表,用A,B,C实例poplualte,然后为对方创建一个任务来调用方法do1() ;
interface I
{
void do1();
}
class A : I
{
public void do1()
{
Console.WriteLine("A");
}
}
class B : I
{
public void do1()
{
Console.WriteLine("B");
}
}
class C : I
{
public void do1()
{
Console.WriteLine("C");
}
}
class Program
{
public static void Main(string[] args)
{
List<I> l = new List<I>();
l.Add(new A());
l.Add(new B());
l.Add(new C());
var TaskPool = new List<Task>();
foreach (var i in l)
{
Task task = …Run Code Online (Sandbox Code Playgroud) List<DocumentDTO> lstDocs = objService
.SelectDocumentInfo()
.Where(l => int.Parse(l.FileName.Substring(0, l.FileName.IndexOf('\'))) = docID)
.ToList();
Run Code Online (Sandbox Code Playgroud)
我的docID是一个int价值
279\Chrysanthemum.jpg
Run Code Online (Sandbox Code Playgroud)
279是我的docID我想只获得那些在指定FileName前面有279的记录,或者如果docID更改为其他内容,那么只能通过LINQ获取这些记录.
我怎样才能做到这一点?
我有 TypeScript 代码,并且需要 C# 中的等效代码。
宣言:
private sessionCommands: SessionCommand[];
// . . .
// Create array in constructor.
this.sessionCommands = new Array();
// . . .
// Push few objects to array in some method
Run Code Online (Sandbox Code Playgroud)
然后获取数据。这是重要的部分,如何在 C# 中做到这一点?
var data = this.sessionCommands.map(x => x.identifier + " " + x.getParameter() + ";").join("\n");
Run Code Online (Sandbox Code Playgroud) 我指的是此处描述的元组文字: https: //blogs.msdn.microsoft.com/dotnet/2016/08/24/whats-new-in-csharp-7-0/#comment-321926
\n\n喜欢元组字面意思。
\n\n然而,我预见到很多人都会在返回元组中查找项目的顺序,并且想知道我们如何解决这个问题。
\n\n例如,将元组中的项目名称作为身份定义方面而不是顺序更有意义吗?或者有没有办法做到这一点我没有看到?
\n\n例如:假设 NextEmployee() 是一些库方法,我没有\xe2\x80\x99t 的源代码,并且\xe2\x80\x99t 没有特别详细的文档记录,假设它返回(firstname: \xe2\x80\x9cjamie\xe2\x80\x9d, lastname: \xe2\x80\x9chince\xe2\x80\x9d, id: 102348)给我,我说:
(string lastname, var _, int __) = NextEmployee(); // I only need the last name\nRun Code Online (Sandbox Code Playgroud)\n\n编译器要么很乐意将名字分配给姓氏,要么对此发出警告或错误。为什么不直接将姓氏映射到姓氏呢?
\n\n我\xe2\x80\x99d 看到允许更松散耦合的架构,如果我们\xe2\x80\x99t 不必记住元组中姓氏的索引,并且可以只要求一个方面\xe2\x80\x9clastname\ xe2\x80\x9d 之类的。
\n我在2016年从.NET C#的全职编程中退休,但最近刚回来作为一个业余爱好者程序员.我有兴趣发现一种使用胖箭头编码属性的新方法:
public DateTime PicDate { get => _picDate; set => _picDate = value; }
Run Code Online (Sandbox Code Playgroud)
这就是我习惯的(以及get; set;事物):
public int Century
{
get
{
return _century;
}
set
{
_century = value;
}
}
Run Code Online (Sandbox Code Playgroud)
除了编码的简易性,实现中是否存在实际的实际差异?C#7是否比旧方式更有效地处理新方法?
换句话说,新语法是"比旧"更好吗?
我必须将其插入到注册表中:
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows CE Services\AutoStartOnConnect]
"AutoRun"="d:\\MyFolder\\MyProgram.exe"
Run Code Online (Sandbox Code Playgroud)
我怎么用C#做这个?
我创建了一个自定义控件并将其绑定到Form.我在控件中绘制图形文本并添加到Form.但它没有显示表格.这是我的代码.
//创建自定义控件
public class DrawTextImage : Control
{
public void DrawBox(PaintEventArgs e, Size size)
{
e.Graphics.Clear(Color.White);
int a = 0;
SolidBrush textColor = new SolidBrush(Color.Black);
using (SolidBrush brush = new SolidBrush(Color.Red))
{
e.Graphics.FillRectangle(brush, new Rectangle(a, a, size.Width, size.Height));
e.Graphics.DrawString("Text", Font, textColor, new PointF(50, 50));
}
}
}
Run Code Online (Sandbox Code Playgroud)
//加载Form1
public Form1()
{
InitializeComponent();
DrawTextImage call = new DrawTextImage();
call.Text = "TextControl";
call.Name = "TextContrl";
Size siz = new Size(200, 100);
call.Location = new Point(0, 0);
call.Visible = true;
call.Size = siz; …Run Code Online (Sandbox Code Playgroud) 为什么是这样:
public int X { get; } = 5
public int Y { get; } = X;
Run Code Online (Sandbox Code Playgroud)
不可能?
因为手动执行:
public TestClass()
{
X = 5;
Y = X;
}
Run Code Online (Sandbox Code Playgroud)
工作,(显然?)这样做:
public static int X { get; } = 5;
public static int Y { get; } = X;
Run Code Online (Sandbox Code Playgroud)
有没有办法让第一个例子进行编译,还是我必须在ctor中手动完成?
(我真正的问题要复杂得多,不仅仅是整数,而是用于创建其他实例的实例,但这个例子更容易讨论)
我正在为Windows 10编写应用程序,需要在UI中显示Time.
我做了这样的显示
Time.Text = DateTime.Now.ToString("h:mm:ss tt");
Run Code Online (Sandbox Code Playgroud)
但我需要更新它,有关如何做到这一点的任何建议吗?
我在运行dotnet watch run时遇到以下错误.
Unhandled Exception: System.FormatException: Could not parse the JSON file.
Error on line number '0': ''. -
--> Newtonsoft.Json.JsonReaderException: Error reading JObject from
JsonReader. Path '', line 0, position 0
at Newtonsoft.Json.Linq.JObject.Load(JsonReader reader, JsonLoadSettings
settings) at
Microsoft.Extensions.Configuration.Json.JsonConfigurationFileParser.Parse(Stream input)
at Microsoft.Extensions.Configuration.Json.JsonConfigurationProvider.Load(Stream stream)
--- End of inner exception stack trace ---
at Microsoft.Extensions.Configuration.FileConfigurationProvider.Load(Boolean reload)
at Microsoft.Extensions.Configuration.ConfigurationRoot..ctor(IList`1 providers)
at Microsoft.Extensions.Configuration.ConfigurationBuilder.Build()
at WebApplicationBasic.Startup..ctor(IHostingEnvironment env) in \Microservices\TestProject\Startup.cs:line 25
Run Code Online (Sandbox Code Playgroud)
我确认json文件没有错.如果有帮助,添加我的项目文件.这可能是无关紧要的,但我不知道在哪里看.
appsettings.json:
{
"ASPNETCORE_ENVIRONMENT": "Development",
"ConnectionStrings": {
"Default": "Server=localhost; database=TestProject; Integrated Security=True"
},
"Logging": { …Run Code Online (Sandbox Code Playgroud) 所以我在C#中构建一个小应用程序,我有一个IEnumerable,我想把它转换成List.这就是我得到的:
var enumerable = SettingsManager.ReadSettings();
var list = enumerable.Cast<Setting>().ToList();
Run Code Online (Sandbox Code Playgroud)
编译器说无法从使用中推断出ReadSettings.这就是ReadSettings的样子:
public static IEnumerable<T> ReadSettings<T>()
{
//Code omitted
return JsonConvert.DeserializeObject<T[]>(fileAsString).ToList<T>();
}
Run Code Online (Sandbox Code Playgroud) 例如,在以下示例中:
string commandText = string.Format("Select * from {0}", filename);
Run Code Online (Sandbox Code Playgroud)
以上是如何工作的?
我有一个字符串C#列表,如下所示:
var list = new List<string>() { "A", "B", "C", "B", "C", "B", "D" };
Run Code Online (Sandbox Code Playgroud)
我想只保留该列表中的唯一项目,并删除多次出现的所有项目.
在这个示例中,在删除所有重复项之后,将剩下项目"A"和"D".
c# ×12
c#-6.0 ×2
linq ×2
.net ×1
asp.net ×1
asp.net-core ×1
c#-4.0 ×1
c#-7.0 ×1
constructor ×1
lambda ×1
linq-to-sql ×1
properties ×1
registry ×1
roslyn ×1
sorting ×1
split ×1
substring ×1
tuples ×1
typescript ×1
uwp ×1
valuetuple ×1
winforms ×1
xaml ×1