Skip to content

Instantly share code, notes, and snippets.

View tany3's full-sized avatar
😇
I may be slow to respond.

Masahiro Taniuchi tany3

😇
I may be slow to respond.
  • Tokyo, Japan
View GitHub Profile
@tany3
tany3 / gist:9245655
Created February 27, 2014 07:03
重複処理 WhereとFirstOrDefault

##重複処理 WhereとFirstOrDefault - Replace with single call to FirstOrDefault()

###変更前

  collection.where(item => item.Equals(value)).FirstOrDefault();

###変更後

 collection.FirstOrDefault(item => item.Equals(value));
@tany3
tany3 / gist:9245669
Created February 27, 2014 07:05
カウントを取らなくてい - Use method Any()

##カウントを取らなくてい - Use method Any() あるcollectionが0件であることを知りたいなら、!Any()を使う。 Any()は有無を返す。Count()は個数を返すので、数え上げ処理が動く。 ###変更前

if (collection.Count() == 0) { ... } else { ... }

###変更後

if (!collection.Any()) { ... } else { ... }
@tany3
tany3 / gist:9245701
Created February 27, 2014 07:08
仕様を明確にする - Field can be readonly

###仕様を明確にする - Field can be readonly 初期化時だけセットするフィールドはreadonlyにすることで、副作用を防ぐことができる(安全弁になる)。

public class Book
{
  private string readonly name;
  private int readonly ISBN;
  
  public Book(string name, int isbn)
 {
@tany3
tany3 / countries
Last active August 29, 2015 13:57
Test data : countries
# Test data
## countries
Afghanistan
Albania
Algeria
Andorra
Angola
Antigua and Barbuda
Argentina
@tany3
tany3 / gist:2d032cbbbd9db04b976d
Created May 15, 2014 08:23
重複処理 IEnumerableをそのまま使い回さない - Possible multiple enumeration of IEnumerable

IEnumerableをそのまま使うと、使う度に毎回変換される。遅延実行。 http://confluence.jetbrains.com/display/ReSharper/Possible+multiple+enumeration+of+IEnumerable

Assuming that GetNames() returns an IEnumerable, we are, effectively, doing extra work by enumerating this collection twice in the two foreach statements. If GetNames() results in a database query, you end up doing that query twice, while both times getting the same data.

ということで、特に複数回使う場合など適宜ToList()やToArray()しておく。

参考:LINQ クエリの概要 (C#) http://msdn.microsoft.com/ja-jp/library/bb397906.aspx

@tany3
tany3 / CSChatworkAPISample.cs
Created December 18, 2014 03:25
CSChatworkAPISample
using System;
using CSChatworkAPI;
namespace Example
{
class Program
{
static void Main(string[] args)
{
var client = new ChatworkClient(@"APIToken"); // TODO:APIToken
@tany3
tany3 / lsWebApps.ps1
Created December 25, 2014 09:18
List of IIS Applications
#
#see http://melcher.it/2013/03/powershell-list-all-iis-webapplications-net-version-state-identity/
#
try{
Import-Module WebAdministration
Get-WebApplication
$webapps = Get-WebApplication
$list = @()
foreach ($webapp in get-childitem IIS:\AppPools\)
@tany3
tany3 / StartWebSItes.ps1
Created December 25, 2014 10:07
PowerShell-StartSites
#
#start web site
#
$targetSiteNames = ( "WebSite1", "WebSite2" )
$targetAppPools = ( "AppPool1", "AppPool2" )
"Settings.Target Sites:"
foreach ($name in $targetSiteNames) { "`t" + $name }
"Settings.Target AppPools:"
foreach ($name in $targetAppPools) { "`t" + $name }
@tany3
tany3 / StopWebSites.ps1
Last active August 29, 2015 14:12
PowerShell-StopSites
#
#stop web site
#
$targetSiteNames = ( "WebSite1", "WebSite2" )
$targetAppPools = ( "AppPool1", "AppPool2" )
"Settings.Target Sites:"
foreach ($name in $targetSiteNames) { "`t" + $name }
"Settings.Target AppPools:"
foreach ($name in $targetAppPools) { "`t" + $name }
@tany3
tany3 / new_gist_file_0
Created January 15, 2015 06:20
VirtualBox resize vdi
c:\Program Files\Oracle\VirtualBox>VBoxManage.exe clonehd "D:\src.vdi" "D:\dst.vdi"
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
Clone hard disk created in format 'VDI'. UUID: ****
c:\Program Files\Oracle\VirtualBox>VBoxManage.exe modifyhd "D:\dst.vdi" --resize 153600
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%