Constraints on Type Parameters
Constraint | Description |
---|---|
where T: struct |
The type argument must be a value type. Any value type except Nullable can be specified. See Using Nullable Types for more information. |
where T : class |
The type argument must be a reference type; this applies also to any class, interface, delegate, or array type. |
where T : new() |
The type argument must have a public parameterless constructor. When used together with other constraints, the new() constraint must be specified last. |
where T : <base class name>
|
The type argument must be or derive from the specified base class. |
where T : <interface name>
|
The type argument must be or implement the specified interface. Multiple interface constraints can be specified. The constraining interface can also be generic. |
where T : U |
The type argument supplied for T must be or derive from the argument supplied for U. |
MemoryMappedFile
For IPC(Inter-Process Communication) use.
About Unit Test Code Coverage
Methods to be tested should be directly called by TestMethod
in unit test project.
Regex - match number groups from string
var matches = new Regex(@"([0-9]+)").Matches(sourceString); //() : group; [0-9] : number; + : one or more times.
var result = new List<int>();
for (int i = 0; i < matches.Count; i++)
{
result.Add(int.Parse(matches[i].Groups[1].Value));
}
return result ;
Add Sorted Item to List
public static void AddSorted<T>(this IList<T> list, T item, IComparer<T> comparer = null)
{
if (comparer == null)
comparer = Comparer<T>.Default;
int i = 0;
while (i < list.Count && comparer.Compare(list[i], item) < 0)
i++;
list.Insert(i, item);
}
What's the differences between Task.Run and Task.Factory.StartNew
So, in the .NET Framework 4.5 Developer Preview, we’ve introduced the new Task.Run method. This in no way obsoletes Task.Factory.StartNew, but rather should simply be thought of as a quick way to use Task.Factory.StartNew without needing to specify a bunch of parameters. It’s a shortcut. In fact, Task.Run is actually implemented in terms of the same logic used for Task.Factory.StartNew, just passing in some default parameters. When you pass an Action to Task.Run:
Task.Run(someAction);
that’s exactly equivalent to:
Task.Factory.StartNew(someAction,
CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default);
For instance:
lets say that you want to create a long running task thread. If a thread of the thread pool is going to be used for this task, then this could be considered an abuse of the thread pool.
One thing you could do in order to avoid this would be to run the task in a separate thread. A newly created thread that would be dedicated to this task and would be destroyed once your task would have been completed. You cannot achieve this with the Task.Run, while you can do so with the Task.Factory.StartNew, like below:
Task.Factory.StartNew(..., TaskCreationOptions.LongRunning);
Linq Select() notice
Thinking about these codes below:
//update method signiture from MyDataService: bool UpdateModel(string id);
var updateResults = MyViewModels.Select(vm => MyDataService.UpdateModel(vm.Model.Id));
if (updateResults.Any(r => r))
{
//if at least one update done, notify user.
}
If there're more than 1 MyViewModel
here, and the first is updated successfully, then the other Models will not be updated. The reason is that Any()
is called to execute Select()
sequence and the first condition has been satisfied. So here we'd better use a ToList()
append to Select()
.
Signal
- SemaphoreSlim
SemaphoreSlim _ss = new SemaphoreSlim(1); //Synchronously only 1 await execution to go
public async Task<string> Request()
{
await _ss.WaitAsync(); // compare with Wait(), it's asynchronous.
var response = await request.GetResponseAsync();
_ss.Release();
}
- EventWaitHandle (no asynchronous operation)
EventWaitHandle _ew = new EventWaitHandle(false, EventResetMode.AutoReset);//initialState is false and reset automatically
public void ShowOneThingForAWhile(string message, int millisecond)
{
if (ShowNotification)
_ew .Set();
Message = message;
ShowNotification = true;
Task.Run(() => {
_ew .WaitOne(millisecond); //will reset automatically here
ShowNotification = false;
});
}
Asynchronous != Multi-threads
JavaScript milliseconds and C# DateTime converters
public static class DateTimeExtensions
{
public static readonly DateTime DtUtc1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
public static long ToJSMilliseconds(this DateTime dt)
{
return ((dt.ToUniversalTime().Ticks - DtUtc1970.Ticks) / 10000);
}
public static DateTime? ParseJSMilliseconds(this string totalMilliseconds)
{
long ms;
if (!long.TryParse(totalMilliseconds, out ms)) return null;
DateTime dt = DtUtc1970.AddMilliseconds(ms).ToLocalTime();
return dt;
}
}