Thursday, 12 February 2015

C# Count Array Elements



ArrayList


Here we get the number of elements in an ArrayList, which is a non-generic collection that stores its length in a property called Count. This is identical to the List collection, which we will see next. This property access is fast.

Program that uses ArrayList: C#

using System;
using System.Collections;

class Program
{
    static void Main()
    {
        ArrayList ar = new ArrayList();
        ar.Add("One");
        ar.Add("Two");

        // Count the elements in the ArrayList.
        int c = ar.Count;
        Console.WriteLine(c);
    }
}

Output

2 

List

Here we use List, a generic collection. You must specify its type in the sharp angle brackets <T>. These collections have superior performance and usually preferable. List has a property called Count that is fast to access.

Program that uses List: C#

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {
        List<int> list = new List<int>();
        list.Add(1);
        list.Add(2);

        // Count with the Count property.
        int c = list.Count;
        Console.WriteLine(c);
    }
}

Output
2

Arrays


Arrays are counted differently and you need to use Length. In programs, an array's size is set differently than List or ArrayList, and it does not grow dynamically. The Length value will include all elements.


Count extension

You can use the Count() extension method when you have an IEnumerable collection or are using LINQ statements. Enumerations in C# are collections that haven't been determined exactly yet, and they can use yield and foreach keywords.
Program that uses LINQ: C#
 
using System;
using System.Linq;
 
class Program
{
    static void Main()
    {
        string[] arr = new string[]
        {
            "One",
            "Two",
            "Three"
        };
 
        // An enumerable collection, not in memory yet.
        var e = from s in arr
               select s;
 
        int c = e.Count();
        Console.WriteLine(c);
    }
}
 
Output
 
3


Encrypt and Decrypt in C# asp.net



In this article I will explain how to encrypt and decrypt a string in ASP.Net.
Below are the functions for Encryption and Decryption which will be used for the Encrypting or Decrypting Username or Password.


private string Encrypt(string clearText)
{
    string EncryptionKey = "MAKV2SPBNI99212";
    byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
    using (Aes encryptor = Aes.Create())
    {
        Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
        encryptor.Key = pdb.GetBytes(32);
        encryptor.IV = pdb.GetBytes(16);
        using (MemoryStream ms = new MemoryStream())
        {
            using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
            {
                cs.Write(clearBytes, 0, clearBytes.Length);
                cs.Close();
            }
            clearText = Convert.ToBase64String(ms.ToArray());
        }
    }
    return clearText;
}

private string Decrypt(string cipherText)
{
    string EncryptionKey = "MAKV2SPBNI99212";
    byte[] cipherBytes = Convert.FromBase64String(cipherText);
    using (Aes encryptor = Aes.Create())
    {
        Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
        encryptor.Key = pdb.GetBytes(32);
        encryptor.IV = pdb.GetBytes(16);
        using (MemoryStream ms = new MemoryStream())
        {
            using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
            {
                cs.Write(cipherBytes, 0, cipherBytes.Length);
                cs.Close();
            }
            cipherText = Encoding.Unicode.GetString(ms.ToArray());
        }
    }
    return cipherText;
}