From the C# documentation it states that the @ symbol is an Identifier. 2.4.2 Identifiers The prefix “@” enables the use of keywords as identifiers, which is useful when interfacing with other programming languages. The character @ is not actually part of the identifier, so the identifier might be seen in other languages as a […]
Was detecting a file stream if it was Unicode encoded or not, so though I would share the code. Let say you an xml file that you need to detect if it has the Unicode BOM in the file. If you wish to know more about BOM look it up at wikipedia
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
//open the helloworld.xml file Stream fs = new FileStream("helloworld.xml", FileMode.Open); byte[] bits = new byte[3]; fs.Read(bits, 0, 3); // UTF8 BOM is like 0xEF,0xBB,0xBF if (bits[0] == 0xEF && bits[1] == 0xBB && bits[2] == 0xBF) { //utf8 do something } //UTF16 BOM is like 0xFF, 0xFE if (bits[0] == 0xFF && bits[1] == 0xFE) { //utf16 do something } |
Hope that […]
Lots of developers confuse the var keyword in C# with JavaScript. The var keyword was introduced in C# 3.0, and it is basically implicitly defined but is statically defined, var are resolved at compile time not at run-time. The confusion is there also a var keyword in JavaScript, but in JavaScript var is dynamically defined. […]
Was going through some legacy code recently and found a big for loop that had multiple switch statement inside. So being that I hate something that is fairly large, every method should be small and concise, I went along to refactor it to SRP. This code is generic so you might see something like this […]
Here is some poorly written code that I had to review today and modify (i.e refactor). I have removed the domain of the code and renamed the variables, so that it remains anonymous, but the just of the code is still intact.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
var itemCount = 0; if (users.ShoppingCart != null && user.ShoppingCart > 0) { foreach (var cart in user.ShoppingCart) { if (cart.Items != null && cart.Items.Count > 0) { foreach (var item in cart.Items) { itemCount++; } } } } |
Lets go through the inner loop of the code first. First the […]
Here is a quick way to detect if a text is in Canadian Aboriginal Syllabic in C#. It is just an extension method that would detect if your string is in that range by using regex.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public static bool IsInuktitut(this string input) { if (string.IsNullOrEmpty(input)) { return false; } //http://msdn.microsoft.com/en-us/library/20bw873z.aspx //look at section Supported Named Blocks const string pattern = @"\p{IsUnifiedCanadianAboriginalSyllabics}"; Match match = Regex.Match(input, pattern); return match.Success; } |
Enjoy.


