可能重复:
您何时在C#中使用委托?
代表们的目的
我看到很多关于代表使用的问题.我仍然不清楚你在哪里和为什么使用代理而不是直接调用方法.
我多次听过这个短语:"然后可以将委托对象传递给可以调用引用方法的代码,而不必在编译时知道将调用哪个方法."
我不明白这句话是怎么回事.
我写了以下例子.假设您有3个具有相同参数的方法:
public int add(int x, int y)
{
int total;
return total = x + y;
}
public int multiply(int x, int y)
{
int total;
return total = x * y;
}
public int subtract(int x, int y)
{
int total;
return total = x - y;
}
Run Code Online (Sandbox Code Playgroud)
现在我宣布一个代表:
public delegate int Operations(int x, int y);
Run Code Online (Sandbox Code Playgroud)
现在我可以更进一步声明一个处理程序来使用这个委托(或你的委托直接)
致电代表:
MyClass f = new MyClass();
Operations p = new Operations(f.multiply);
p.Invoke(5, 5);
Run Code Online (Sandbox Code Playgroud)
或者使用处理程序调用
f.OperationsHandler = …Run Code Online (Sandbox Code Playgroud) 我有一个使用ASP.NET成员资格提供程序的基本应用程序 默认情况下,您可以使用一些字段,如用户名,密码,记住我.
如何添加到asp.net成员资格提供程序,以便我可以在寄存器部分添加一个额外的字段"地址"并将其保存到数据库中?
在帐户模型中创建用户时,我目前看到以下内容:
public MembershipCreateStatus CreateUser(string userName, string password,
string email)
{
if (String.IsNullOrEmpty(userName))
{ throw new ArgumentException("Value cannot be null or empty.", "userName"); }
if (String.IsNullOrEmpty(password))
{ throw new ArgumentException("Value cannot be null or empty.", "password"); }
if (String.IsNullOrEmpty(email))
{ throw new ArgumentException("Value cannot be null or empty.", "email"); }
MembershipCreateStatus status;
_provider.CreateUser(userName, password, email, null, null, true,
null, out status);
return status;
}
Run Code Online (Sandbox Code Playgroud)
在创建用户功能中,我还想保存用户的"地址".
在register.aspx中,我添加了以下内容:
<div class="editor-label">
<%: Html.LabelFor(m => m.Address) %>
</div>
<div class="editor-field">
<%: Html.TextAreaFor(m …Run Code Online (Sandbox Code Playgroud) 我目前有一个TextBox使用:
<%: Html.TextBox("TextBox1") %>
如何获取TextBox作为字符串输入的内容的值,以便我可以在整个应用程序中使用该字符串变量?
该视图让他跟随页面顶部的继承进行建模.此页面名为"InputNumbersSection":
<%: Html.TextBoxFor(m => m.Number) %>
Run Code Online (Sandbox Code Playgroud)
和行动:
<%: Html.ActionLink("Get Number!", "DisplayNumbersSection") %>
Run Code Online (Sandbox Code Playgroud)
模型有这个:
public class NumberModels
{
public string Number { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
控制器具有以下内容:
public ActionResult DisplayNumbersSection(NumberModels model)
{
if (ModelState.IsValid)
{
string TextBoxValue = model.Number;
ViewData["Number"] = TextBoxValue;
}
return View();
}
Run Code Online (Sandbox Code Playgroud)
我在另一个页面中使用的ViewData返回视图中键入的文本框中的数字.
当我在文本框中键入somthing时,我看不到该属性被击中或执行."Number"属性始终返回NULL.它似乎似乎没有把我在TextBox中键入的内容拿起来
我有一个ASP.NET MVC3应用程序,它从WCF服务获取坐标列表.此服务返回具有一些属性的列表(Xcoor和Ycoor).
public List<Location> GetCoordinate()
{
sensor = new Location();
List<Location> coordinateList = sensor.GetSensorInformation();
return coordinateList;
}
Run Code Online (Sandbox Code Playgroud)
在我的网页中,我有一个JavaScript函数,它接受2个参数并在页面上显示坐标(在地图上)
function GetLocation(y, x){
//my code here
}
Run Code Online (Sandbox Code Playgroud)
我想从我的控制器获取coordiates列表,并将它们传递给JS函数进行处理.
最好的方法是什么?
我尝试使用以下,但我不知道如何解析结果
$.ajax({
url: '@Url.Action("GetCoordinate")',
type: 'GET',
dataType: 'xml',
success: function(result) {
},
error: function (xhr, ajaxOptions, thrownError){
alert(xhr.status);
alert(thrownError);}
});
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我正在尝试并行编程.我有一个正常的循环:
for (int i = 0; i < 100000; i++)
{
Console.WriteLine("Thread ID: {0} and i is " + i + " Time Elapsed: " + sw.Elapsed,
Thread.CurrentThread.ManagedThreadId);
}
Run Code Online (Sandbox Code Playgroud)
这个循环只是将数字增加到100000
我可以使用此for循环并将其转换为Parallel.For循环以将数字计数到100000但是并行使用所有CPU吗?
此外,使用a时Parallel.For,需要哪些参数?你会如何以一种非常基本的方式使用它?
我有一个示例应用程序,并想知道是否有人可以阐明这一点.当我在for循环上放置一个断点进入调试器中的代码时,为什么它从一个线程切换到另一个?在运行时运行应用程序时会这样做...请参阅下面的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TPLSample
{
class Program
{
static void Main(string[] args)
{
Management m = new Management();
Task a = new Task(() => m.Operation1());
a.Start();
Task b = new Task(() => m.Operation2());
b.Start();
Console.ReadLine();
}
}
public class Management
{
A a = null;
B b = null;
public void Operation1()
{
a = new A();
}
public void Operation2()
{
b = new B();
}
}
public class A …Run Code Online (Sandbox Code Playgroud) c# ×3
asp.net-mvc ×2
asp.net ×1
c#-4.0 ×1
delegates ×1
javascript ×1
jquery ×1
locking ×1
parallel-for ×1