.net-4.0

Should I notice a difference in using Task vs Threads in .Net 4.0?

99封情书 提交于 2019-12-20 11:36:30
问题 I updated my code to use Tasks instead of threads.... Looking at memory usage and CPU I do not notices any improvements on the multi-core PC, Is this expected? My application essentially starts up threads/tasks in different objects when it runs... All I'm doing is a simple Task a = new Task(...) a.Start(); 回答1: There are various implications to using Tasks instead of Threads, but performance isn't a major one (assuming you weren't creating huge numbers of threads.) A few key differences: The

CallerMemberName in .NET 4.0 not working

别来无恙 提交于 2019-12-20 11:21:50
问题 I am trying to use CallerMemberName attribute in .NET 4.0 via BCL portability pack. It is always returning an empty string instead of the member name. What am I doing wrong? public partial class Form1 : Form { public Form1() { InitializeComponent(); MessageBox.Show(new class2().CallMe); } } public class class2 { public string CallMe { get { return HelpMe(); } } private string HelpMe([CallerMemberName] string param = "") { return param; } } 回答1: Targeting 4.0 works just fine if you add:

CallerMemberName in .NET 4.0 not working

时光怂恿深爱的人放手 提交于 2019-12-20 11:20:48
问题 I am trying to use CallerMemberName attribute in .NET 4.0 via BCL portability pack. It is always returning an empty string instead of the member name. What am I doing wrong? public partial class Form1 : Form { public Form1() { InitializeComponent(); MessageBox.Show(new class2().CallMe); } } public class class2 { public string CallMe { get { return HelpMe(); } } private string HelpMe([CallerMemberName] string param = "") { return param; } } 回答1: Targeting 4.0 works just fine if you add:

What is a practical usage of Code Contracts in .NET 4.0?

倾然丶 夕夏残阳落幕 提交于 2019-12-20 10:19:04
问题 In order to fully understand and take advantage of the new features and enhancements provided with the coming of the new .NET Framework 4.0, I would like to get an example of real-world application of Code Contracts. Anyone has a good example of application of this feature? I would like to get a code sample with a brief explanation to help me get up and running with it. 回答1: From The Code Contracts User Manual: Contracts allow you to express preconditions, postconditions and object invariants

How to get version number in post-build event

早过忘川 提交于 2019-12-20 09:58:26
问题 I want to use post-build event to automatically create a nuget package and then copy it to a shared folder on our network, something like this (the version number 1.0.0.0. is specified inside the MyLib.nuspec file): nuget.exe pack "$(SolutionDir)MyLib.nuspec" xcopy /Y "$(TargetDir)MyLib.1.0.0.0.nupkg" \\folder\subfolder\NuGetPackages This works, but now I would like to update this script, so that it would include the assembly version also, but I cannot get assembly version inside the post

Lazy initialization in .NET 4

南笙酒味 提交于 2019-12-20 09:47:06
问题 What is lazy initialization. here is the code i got after search google. class MessageClass { public string Message { get; set; } public MessageClass(string message) { this.Message = message; Console.WriteLine(" *** MessageClass constructed [{0}]", message); } } Lazy<MessageClass> someInstance = new Lazy<MessageClass>( () => new MessageClass("The message") ); why should i create object in this way....when actually we need to create object in this way......looking for answer. 回答1: The purpose

What is difference between Task and Thread?

a 夏天 提交于 2019-12-20 08:58:03
问题 Today I was digging with TPL and found a new class Task.Now I just wanted to know that what is diffrence between task and Thread,and which one is better? 回答1: what is diffrence between a task and a thread? Suppose you are running a book delivery company. You have four cars and four drivers. A car is a thread, a driver is a processor, and a book delivery is a task. The problem you face is how to efficiently schedule the drivers and cars so that the tasks get done as quickly as possible. Where

Accessing a non-static member via Lazy<T> or any lambda expression

戏子无情 提交于 2019-12-20 08:56:08
问题 I have this code: public class MyClass { public int X { get; set; } public int Y { get; set; } private Lazy<int> lazyGetSum = new Lazy<int>(new Func<int>(() => X + Y)); public int Sum{ get { return lazyGetSum.Value; } } } Gives me this error: A field initializer cannot reference the non-static field, method, or property. I think it is very reasonable to access a non-static member via lazy, how to do this? * EDIT * The accepted answer solves the problem perfectly, but to see the detailed and

Is System.Web.Caching or System.Runtime.Caching preferable for a .NET 4 web application

和自甴很熟 提交于 2019-12-20 08:39:50
问题 I am adding caching to an ASP.NET web application. This is .NET 4, so I can use the classes in the System.Runtime.Caching namespace (which, as I understand it, was added to provide similar functionality to that found in System.Web.Caching, but for non-Web-apps.) But since this is a web app, am I better off using System.Web.Caching? Or is the newer System.Runtime.Caching superior in some way? 回答1: Microsoft recommends using System.Runtime.Caching for all caching purposes. See this: http://msdn

Whats wrong in this Parallel.For Code?

旧街凉风 提交于 2019-12-20 07:14:37
问题 this is the code that i want to run. Parallel.For(1, itemCount, 1, () => { return new ThreadLocalStateCache() { //assigning values to local variables Receipient = serMailObj.ReceipientList.Dequeue(), //get a single recepeint for the email mail = serMailObj.Email, //Object of type MailMessage client = client //object of type SmtpClient }; } , (i, loopState) => { doWork(i, loopState.ThreadLocalState); }); } //class to store local vairables for each thread public class ThreadLocalStateCache {