This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class MaxHeap | |
| { | |
| private int[] heap = new int[1000]; // use 1 as the start index to record the heap elements | |
| private int heapSize = 0; | |
| public MaxHeap() { } | |
| public void Push(int x) | |
| { | |
| heap[++heapSize] = x; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| app.Run(async context => | |
| { | |
| // 显示使用异步AsyncDispose来调用异步FlushAsync() | |
| await using (var streamWriter = new StreamWriter(context.Response.Body)) | |
| { | |
| await streamWriter.WriteAsync("Hello World"); | |
| } | |
| }); | |
| app.Run(async context => |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # method1 | |
| if(!context.Response.HasStarted) | |
| { | |
| context.Response.Headers["test"] = "value"; | |
| } | |
| # method2 | |
| context.Response.OnStarting(()=> | |
| { | |
| context.Response.Headers["someheader"] = "somevalue"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 使用HttpCompletionOption.ResponseHeadersRead避免缓冲整个响应体至内存中 | |
| using (var response = await _client.GetAsync(_url, HttpCompletionOption.ResponseHeadersRead)) | |
| { | |
| // 获取响应流 | |
| var responseStream = await response.Content.ReadAsStreamAsync(); | |
| // 创建StreamReader、JsonTextReader | |
| // 做了一些缓冲,不是双缓冲 | |
| var textReader = new StreamReader(responseStream); | |
| var reader = new JsonTextReader(textReader); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class RoleManager<TRole> :IDisposable where TRole : class | |
| { | |
| private bool _disposed; | |
| public virtual CancellationToken CancellationToken => CancellationToken.None; | |
| public void Dispose() | |
| { | |
| Dispose(true); | |
| GC.SuppressFinalize(this); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Result | |
| { | |
| private static readonly Result _success = new Result { Succeeded = true }; | |
| private List<Error> _errors = new List<Error>(); | |
| public bool Succeeded { get;protected set;} | |
| public IEnumerable<Error> Errors => _errors; | |
| public static Result Success => _success; | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # internal 仅在程序集内部使用 | |
| internal static class LoggingExtensions | |
| { | |
| private static readonly Action<ILogger,Exception> _invalidException; | |
| static LoggingExtensions() | |
| { | |
| _invalidException = LoggerMessage.Define(eventId:new EventId(0,"InvalidExceptionOccur"), | |
| logLevel:LogLevel.Debug, | |
| formatString:"ValidateAsync Failed: did not find expect '{Actual}' purpose"); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # good example | |
| public void ConfigureServices(IServiceCollection services) | |
| { | |
| services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme) | |
| .AddCookie(); | |
| // 通过配置选项来解决强依赖关系 | |
| services.AddOptions<CookieAuthenticationOptions>( | |
| CookieAuthenticationDefaults.AuthenticationScheme) | |
| .Configure<IMyService>((options, myService) => |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static class TaskExtensions | |
| { | |
| public static async Task RunAndForget( | |
| this Task task,Action<Exception> onException =null) | |
| { | |
| try | |
| { | |
| await task; | |
| } | |
| catch(Exception ex) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class NetworkConnection : IDisposable | |
| { | |
| string _networkName; | |
| public NetworkConnection(string networkName,NetworkCredential credentials) | |
| { | |
| _networkName = networkName; | |
| var netResource = new NetResource() | |
| { | |
| Scope = ResourceScope.GlobalNetwork, | |
| ResourceType = ResourceType.Disk, |
NewerOlder