在首先描述我的问题之前,我想定义Decorator和Extension方法Decorator的定义
动态地将附加职责附加到对象.装饰器为子类化提供了灵活的替代扩展功能
扩展方法
扩展方法使您可以向现有类型"添加"方法,而无需创建新的派生类型,重新编译或以其他方式修改原始类型
我在c#中有以下代码片段
public interface IMyInterface
{
void Print();
}
public static class Extension
{
public static void PrintInt(this IMyInterface myInterface, int i)
{
Console.WriteLine
("Extension.PrintInt(this IMyInterface myInterface, int i)");
}
public static void PrintString(this IMyInterface myInterface, string s)
{
Console.WriteLine
("Extension.PrintString(this IMyInterface myInterface, string s)");
}
}
public class Imp : IMyInterface
{
#region IMyInterface Members
public void Print()
{
Console.WriteLine("Imp");
}
#endregion
}
class Program
{
static void Main(string[] args)
{
Imp obj = …Run Code Online (Sandbox Code Playgroud) 我是疯了还是有办法在C#中将变量设置为开关的结果?就像是:
var a = switch(b)
{
case c:
d;
case e:
f;
default:
g;
};
Run Code Online (Sandbox Code Playgroud)
是否可以使用任何其他语言?我只是认为它是,但我没有得到任何编译.提前致谢.
我有一个CheckBoxList,我正在填充数据.当我尝试从列表中检索已检查的项目时,我只能获取项目序号,我无法获得该值.
我已经读过你可以使用Items [i] .Value但是当我尝试这样做时,我得到一个错误,指出没有扩展方法'value'.
这是我用来尝试获取信息的代码(注意GetItemText(i)实际上只给我项目位置,而不是项目的文本)
private void btnGO_Click(object sender, EventArgs e)
{
for (int i = 0; i < chBoxListTables.Items.Count; i++)
{
if (chBoxListTables.GetItemChecked(i))
{
string str = chBoxListTables.GetItemText(i);
MessageBox.Show(str);
//next line invalid extension method
chBoxListTables.Items[i].value;
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是使用.Net 4.0
任何想法将不胜感激...谢谢
我在c#中有以下代码
class Test
{
public static int X = Y;
public static int Y = 3;
}
static void Main()
{
Console.WriteLine(Test.X);
Console.WriteLine(Test.Y);
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我得到0和3,但在下面的情况下,我得到3,3
class Test
{
public static int X = 3;
public static int Y = X;
}
static void Main()
{
Console.WriteLine(Test.X);
Console.WriteLine(Test.Y);
}
Run Code Online (Sandbox Code Playgroud)
为什么会这样?
我有以下sql查询找到第二个最高薪水.
Select * From Employee E1 Where
(2) = (Select Count(Distinct(E2.Salary)) From Employee E2 Where
E2.Salary > E1.Salary)
我想将其转换为Linq语句.
我在服务中有一个WCF服务和一个名为GetStudentList()的方法.当它返回单个响应时工作正常.这样的事情
[WebGet(ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
List<Student> GetStudentList();
Run Code Online (Sandbox Code Playgroud)
但我想返回多个响应,即xml和json都是这样的
[WebGet(ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
[WebGet(ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
List<Student> GetStudentList();
Run Code Online (Sandbox Code Playgroud)
有可能吗?如果是,那怎么样?