Thứ Sáu, 11 tháng 4, 2014

Sự khác nhau giữa Struct và Class trong C#

Structs share most of the same syntax as classes, although structs are more limited than classes:

  • Within a struct declaration, fields cannot be initialized unless they are declared as const or static.
  • A struct cannot declare a default constructor (a constructor without parameters) or a destructor.
  • Structs are copied on assignment. When a struct is assigned to a new variable, all the data is copied, and any modification to the new copy does not change the data for the original copy. This is important to remember when working with collections of value types such as Dictionary<string, myStruct>.
  • Structs are value types and classes are reference types.
  • Unlike classes, structs can be instantiated without using a new operator.
  • Structs can declare constructors that have parameters.
  • A struct cannot inherit from another struct or class, and it cannot be the base of a class. All structs inherit directly from System.ValueType, which inherits from System.Object.
  • A struct can implement interfaces.
  • A struct can be used as a nullable type and can be assigned a null value.

Thứ Tư, 26 tháng 3, 2014

Mã hóa HMACSHA256

    private string HMACSHA256(string message, string secret)
    {
        secret = secret ?? "";
        var encoding = new System.Text.ASCIIEncoding();
        byte[] keyByte = encoding.GetBytes(secret);
        byte[] messageBytes = encoding.GetBytes(message);
        using (var hmacsha256 = new HMACSHA256(keyByte))
        {
            byte[] hashmessage = hmacsha256.ComputeHash(messageBytes);
            string sbinary = "";
            for (int i = 0; i < hashmessage.Length; i++)
            {
                sbinary += hashmessage[i].ToString("x2"); // hex format
            }
            return sbinary;
        }
    }

Thứ Sáu, 7 tháng 3, 2014

Gửi 1 file word qua máy in để in sử dụng c#

string[] files = Directory.GetFiles(Environment.CurrentDirectory, "*.docx");
foreach (var file in files)
{
 System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo(file);
 info.Arguments = "\"" + printDialog1.PrinterSettings.PrinterName + "\"";
 info.CreateNoWindow = true;
 info.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
 info.UseShellExecute = true;
 info.Verb = "PrintTo";
 System.Diagnostics.Process.Start(info);
}

Thứ Tư, 27 tháng 2, 2013

So khớp với ký tự bất kỳ và tính luôn cả ký tự xuống dòng \n trong Regex C#

Sử dụng RegexOptions.Singleline

Mình muốn lấy giá trị nằm trong 2 đoạn <tag> và </tag>

string value = Regex.Match("<tag>av</tag>", @"<tag>(.*?)</tag>").Groups[1].Value;

Kết quả: value = 'av'

Đoạn code trên sẽ chạy ok nếu giá trị nằm trong <tag> và </tag> không có ký tự \n. Vì (.*?) chỉ khớp với các ký tự bất kỳ trừ ký tự \n.

string value = Regex.Match("<tag>av\n</tag>", @"<tag>(.*?)</tag>").Groups[1].Value;

Kết quả : value =''

Sử dụng option: RegexOptions.Singleline sẽ làm cách dùng ký tự  dot (.) tính luôn cả giá trị \n.

Specifies single-line mode. Changes the meaning of the dot (.) so it matches every character (instead of every character except \n).

string value = Regex.Match("<tag>av\n</tag>", @"<tag>(.*?)</tag>", RegexOptions.Singleline).Groups[1].Value;

Kết quả: value= 'av\n'

Thứ Sáu, 1 tháng 2, 2013

Các ký tự dùng trong Regex

Các ký tự dùng trong Regex:

. : đại diện cho 1 ký tự bất kỳ trừ ký tự xuống dòng \n.
\d : ký tự chữ số tương đương [0-9]
\D : ký tự ko phải chữ số
\s : ký tự khoảng trắng tương đương [ \f\n\r\t\v]
\S : ký tự không phải khoảng trắng tương đương [ ^\f\n\r\t\v]
\w : ký tự word (gồm chữ cái và chữ số, dấu gạch dưới _ ) tương đương [a-zA-Z_0-9]
\W : ký tự không phải ký tự word tương đương [^a-zA-Z_0-9]
^ : bắt đầu 1 chuỗi hay 1 dòng
$ : kết thúc 1 chuỗi hay 1 dòng
\A : bắt đầu 1 chuỗi
\z : kết thúc 1 chuỗi
| : ký tự ngăn cách so trùng tương đương với phép or (lưu ý cái này nếu muốn kết hợp nhiều điều kiện)
[abc] : khớp với 1 ký tự nằm trong nhóm là a hay b hay c.
[a-z] so trùng với 1 ký tự nằm trong phạm vi a-z, dùng dấu - làm dấu ngăn cách.
[^abc] sẽ không so trùng với 1 ký tự nằm trong nhóm, ví dụ không so trùng với a hay b hay c.
() : Xác định 1 group (biểu thức con) xem như nó là một yếu tố đơn lẻ trong pattern .ví dụ ((a(b))c) sẽ khớp với b, ab, abc.
? : khớp với đứng trước từ 0 hay 1 lần. Ví dụ A?B sẽ khớp với B hay AB.
* : khớp với đứng trước từ 0 lần trở lên . A*B khớp với B, AB, AAB
+ : khớp với đứng trước từ 1 lần trở lên. A+B khớp với AB, AAB.
{n} : n là con số, Khớp đúng với n ký tự đúng trước nó . Ví dụ A{2}) khớp đúng với 2 chữ A.
{n, } : khớp đúng với n ký tự trở lên đứng trước nó , A{2,} khớp vói AA, AAA ...
{m,n} : khớp đùng với từ m->n ký tự đứng trước nó, A{2,4} khớp vói AA,AAA,AAAA.