给出两个接口:
interface I1 {
int Foo();
}
interface I2 {
void Foo();
}
Run Code Online (Sandbox Code Playgroud)
一节课:
class Test : I1, I2 {
int I1.Foo() {
Console.WriteLine("I1.Foo");
return default(int);
}
public void Foo() {
Console.WriteLine("I2.Foo");
}
}
Run Code Online (Sandbox Code Playgroud)
我怎么能扩展接口I2与I1通过保持命名的方法Foo?
我尝试了以下代码,但它没有编译:
interface I1 {
int Foo();
}
interface I2 : I1 {
void I2.Foo();
}
class Test : I2 { /* same code */ }
Run Code Online (Sandbox Code Playgroud) 我想知道为什么当我们 p = new Person("TOM", 999);通过调用 fred.PrintInfo();
它没有改变p到TOM和999,但通过使用p.age = 99; 我们可以很好地改变fred的年龄,构造函数和属性都是公共的,那么我在这里缺少什么?我不想对这段代码做任何事我只想要原因.
using System;
class Person
{
public string fullName;
public int age;
public Person(string n, int a)
{
fullName = n;
age = a;
}
public void PrintInfo()
{
Console.WriteLine("{0} is {1} years old", fullName, age);
}
}
class MainClass
{
public static void SendAPersonByValue(Person p)
{
p.age = 99;
p = new Person("TOM", 999);
}
public static void Main()
{
Person fred = new Person("Fred", 12);
fred.PrintInfo();
SendAPersonByValue(fred); …Run Code Online (Sandbox Code Playgroud) 这是我的Xml文件:
<?xml version="1.0" encoding="utf-8" ?>
<dati>
<product id="456">
<item>a</item>
<item>b</item>
<item>c</item>
</product>
<product id="789">
<item>a</item>
<item>b</item>
</product>
<product id="533">
<item>a</item>
</product>
</dati>
Run Code Online (Sandbox Code Playgroud)
下面的代码只返回第一个item.InnerText元素
List<string> lst = new List<string>();
XDocument Doc = XDocument.Load("test.xml");
var q = from c in Doc.Descendants("product")
where c.Attribute("id").Value == "789"
select c.Element("item");
foreach (string name in q)
lst.Add(name);
listBox1.DataSource = lst;
Run Code Online (Sandbox Code Playgroud)
如何收集所选产品的所有商品?
我试图将这个foreach循环移动到linq:
compData = componentData[0];
foreach (var componentTraceData in componentData)
{
if (!string.IsNullOrEmpty(componentTraceData.CompName))
{
compData = componentTraceData;
break;
}
}
Run Code Online (Sandbox Code Playgroud)
这就是我试过的:
var tt = (from n in componentData
where !string.IsNullOrEmpty(n.CompName)
select n).FirstOrDefault();
Run Code Online (Sandbox Code Playgroud)
componentData[0]如果linq没有找到任何结果我怎么能接受?
我使用 itextsharp 库和 c# 创建了一个带有图像的 pdf。现在我需要在那个图像上有一个超链接,这样当点击它时,它就会转到一个特定的站点。我该怎么做?我试图找到链接到图像对象的属性,但找不到。
我将a转换byte array为a string,然后将其转换string为byte array.这两个字节数组是不同的.
如下:
byte[] tmp = Encoding.ASCII.GetBytes(Encoding.ASCII.GetString(b));
Run Code Online (Sandbox Code Playgroud)
假设b是一个字节数组.
b [0] = 3,b [1] = 188,b [2] = 2 //十进制
结果:
tmp [0] = 3,tmp [1] = 63,tmp [2] = 2
这就是我的问题,它有什么问题?
我正在尝试创建一个可以以另一种形式访问的委托事件。但主窗体看不到我的代表。它说此时代表名称无效。情态形式
public partial class GameOverDialog : Window
{
public delegate void ExitChosenEvent();
public delegate void RestartChosenEvent();
public GameOverDialog()
{
InitializeComponent();
}
private void closeAppButton_Click(object sender, RoutedEventArgs e)
{
ExitChosenEvent exitChosen = Close;
exitChosen();
Close();
}
private void newGameButton_Click(object sender, RoutedEventArgs e)
{
RestartChosenEvent restart = Close;
restart();
Close();
}
}
Run Code Online (Sandbox Code Playgroud)
主要形式:
private void ShowGameOver(string text)
{
var dialog = new GameOverDialog { tb1 = { Text = text } };
dialog.RestartChosenEvent += StartNewGame();
dialog.Show();
}
private void StartNewGame()
{
InitializeComponent(); …Run Code Online (Sandbox Code Playgroud) 我开始学习继承,我想知道在哪里编写派生类.它应该与基类或其他.cs文件位于相同的.cs文件中吗?关于这一点的共识或规范是什么?它甚至重要吗?
我有一张表格.我想做的就是在另一个表单上显示该表单的一部分.我不希望它功能性或任何东西.我基本上只是希望它是一张照片.这有可能,如果是的话,怎么样?
喜欢
display = new display(form, new rectangle(X1, X2, Y1, Y2));
Run Code Online (Sandbox Code Playgroud)
这有可能吗?
我尝试使用async/await功能从XElement对象编写xml文件.但我意识到XElement.Save()不能与async/await一起运行.
也许解决方案可以是使用XElement.Save(Stream)和FileStream对象...
所以,我写了一些如下代码,但很难用文件流处理.
public async Task SaveAsync(XElement xml, string filename)
{
using (var fs = new FileStream(filename, FileMode.Create))
{
xml.Save(fs);
await fs.WriteAsync(**please_help_me**);
}
}
Run Code Online (Sandbox Code Playgroud)
如何处理这种方法还是有其他解决方案?
c# ×10
.net-4.0 ×1
async-await ×1
base ×1
c#-5.0 ×1
delegates ×1
encoding ×1
filestream ×1
inheritance ×1
interface ×1
itextsharp ×1
linq ×1
linq-to-xml ×1
modal-dialog ×1
winforms ×1
wpf ×1
xelement ×1
xml ×1