C# 零碎笔

系统全局资源使用
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;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值