ラベル C# の投稿を表示しています。 すべての投稿を表示
ラベル C# の投稿を表示しています。 すべての投稿を表示

2018年1月26日金曜日

C# (net framework 4.5以降) zip fileを扱う

using System;
using System.IO;
using System.IO.Compression; //このLibを使う

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string startPath = @"c:\example\start";
            string zipPath = @"c:\example\result.zip";
            string extractPath = @"c:\example\extract";

            ZipFile.CreateFromDirectory(startPath, zipPath);

            ZipFile.ExtractToDirectory(zipPath, extractPath);
        }
    }
}

ちなみに、「@」を利用するとエスケープを行わずに「/」を文字列に含められるので、パスを指定したい場合などに可読性が高く、便利に使える。

2018年1月24日水曜日

C# delegate

A delegate is like a pointer to a function. It is a reference type data type and it holds the reference of a method.
delegate は代表の意味である。定義と同じ戻り値と引数を持つMethodを代入できる。

class Program
{
    // declare delegate
    public delegate void Print(int value);

    static void Main(string[] args)
    {
        // Print delegate points to PrintNumber
        Print printDel = PrintNumber;
         
        printDel(100000);
        printDel(200);

        // Print delegate points to PrintMoney
        printDel = PrintMoney;

        printDel(10000);
        printDel(200);
    }

    public static void PrintNumber(int num)
    {
        Console.WriteLine("Number: {0,-12:N0}",num);
    }

    public static void PrintMoney(int money)
    {
        Console.WriteLine("Money: {0:C}", money);
    }
}

A method can have a parameter of a delegate type and can invoke the delegate parameter.

public static void PrintHelper(Print delegateFunc, int numToPrint)
{
    delegateFunc(numToPrint);
}

参考:http://www.tutorialsteacher.com/csharp/csharp-delegates

2017年12月12日火曜日

C# Array

int[] a1 = new int[10]; //10 elements
int[,] a2 = new int[10, 5]; //二次、50 (10 × 5) elements
int[,,] a3 = new int[10, 5, 2]; //三次、100 (10 × 5 × 2) elements

// An array with elements of an array type is sometimes called a jagged array because the lengths of the element arrays do not all have to be the same.
// creates an array with three elements, each of type int[] and each with an initial value of null
int[][] a = new int[3][];
a[0] = new int[10];
a[1] = new int[5];
a[2] = new int[20];

// allocates and initializes an int[] with three elements. the length of the array is inferred from the number of expressions between { and }
int[] a = new int[] {1, 2, 3};

// Local variable and field declarations can be shortened further such that the array type does not have to be restated
int[] a = {1, 2, 3};