您的位置:首页 > 编程语言 > C#

C# 零碎笔

2014-03-06 14:24 204 查看
系统全局资源使用

1. 定义全局资源文件 resource.resx

2. 定义 ResourceManager 实例, 用该实例获取全局资源

ResourceManager rm = new ResourceManager("WspSharedQueue.WspSharedQueue", Assembly.GetExecutingAssembly());

var timeout=rm.GetString("EnqueueTimeout");

3. 接口使用泛型

Interface IEquatable<T>

{

bool Equal(T obj);

}

接口实现

public class Car: IEquatable<Car>

{

public string Name{get;set;}

public string Model{get;set;}

public bool Equal(Car car)

{

if(car.Name==Name && car.Model==Model)

{

return true;

}

return false;

}

}

4. Func 与 Action

声明

Task.Factory.StartNew(Func<T> function).ContinueWith(Action<Task<TResult>> ContinuacteAction);

使用

Task.Factory.StartNew(()=>{doSomething; return t}).ContinueWith(t=>{}, TaskContionOptions.OnlyOnFaulted);

一个教科书般的Task, Timer应用

public static class TimerTaskFactory
	{
		private static readonly TimeSpan DoNotRepeat = TimeSpan.FromMilliseconds(-1);

		/// <summary>
		/// Starts a new task that will poll for a result using the specified function, and will be completed when it satisfied the specified condition.
		/// </summary>
		/// <typeparam name="T">The type of value that will be returned when the task completes.</typeparam>
		/// <param name="getResult">Function that will be used for polling.</param>
		/// <param name="isResultValid">Predicate that determines if the result is valid, or if it should continue polling</param>
		/// <param name="pollInterval">Polling interval.</param>
		/// <param name="timeout">The timeout interval.</param>
		/// <returns>The result returned by the specified function, or <see langword="null"/> if the result is not valid and the task times out.</returns>
		public static Task<T> StartNew<T>(Func<T> getResult, Func<T, bool> isResultValid, TimeSpan pollInterval, TimeSpan timeout)
		{
			Timer timer = null;
			TaskCompletionSource<T> taskCompletionSource = null;
			DateTime expirationTime = (timeout == TimeSpan.MaxValue ? DateTime.UtcNow : DateTime.UtcNow.Add(timeout));

			timer =
				new Timer(_ =>
				{
					try
					{
						if (timeout != TimeSpan.MaxValue && DateTime.UtcNow > expirationTime)
						{
							timer.Dispose();
							taskCompletionSource.SetResult(default(T));
							return;
						}

						var result = getResult();

						if (isResultValid != null && isResultValid(result))
						{
							timer.Dispose();
							taskCompletionSource.SetResult(result);
						}
						else
						{
							// try again
							timer.Change(pollInterval, DoNotRepeat);
						}
					}
					catch (Exception e)
					{
						timer.Dispose();
						taskCompletionSource.SetException(e);
					}
				});

			taskCompletionSource = new TaskCompletionSource<T>(timer);

			timer.Change(pollInterval, DoNotRepeat);

			return taskCompletionSource.Task;
		}

		
	}


enum, struct 是值类型, 有默认值

多次使用+=加事件会导致多次调用回调函数.

每创建一个string对象就会分配一块内存. 如果有很多string对象很容易占用内存. 对于相同的string对象, 会放在字符串池中, 调用Equal返回true, 这是string类自身的实现, 不是说引用同一个地址.

引用类型私有变量和参数指向的是同一个地址,但是提升了参数的访问范围.

private List<string> flows;

public void SetFlows(List<string> flows)

{

this.flows=flows;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: