Shalvin Interests

Thursday, January 27, 2011

Asp .Net Interview Question

1. What are System.Web.Mail and System.Net.Mail?

System.Web.Mail has been deprecated, use System.Net.Mail instead.

System.Net.Mail namespace is used to send electronil mail to Simple Mail Transfer Protocol (SMTP) server for delivery.

2. What is generics?
Generic is a language feature introduced in .Net 2.0. Generics introduce to the .NET Framework the concept of type parameters, which make it possible to design classes and methods that defer the specification of one or more types until the class or method is declared and instantiated by client code. Use of generics makes your code type safe. It is a very efficient way to work with collections.

C# does not allow non-type template parameters.

C# does not support explicit specialization; that is, a custom implementation of a template for a specific type.

C# does not allow the type parameter to be used as the base class for the generic type.

In C#, a generic type parameter cannot itself be a generic, although constructed types can be used as generics. C++ does allow template parameters.

Generics and Collection Initializer in .Net

3. What is boxing and unboxing?

Boxing is the process of converting a value type to the type object. When the CLR boxes a value type, it wraps the value inside a System.Object and stores it on the managed heap.
Unboxing extracts the value type from the object. Boxing is implicit; unboxing is explicit. The concept of boxing and unboxing underlies the C# unified view of the type system, in which a value of any type can be treated as an object.
.NET Interview Questions You'll Most Likely Be Askedint i = 12;

object o = i; 
 

 Unboxing

o = 12;
i = (int)o; 


4. What is the difference between DataRowCollection.Remove method and DataRowCollection.Delete method?

DataRowCollection.Remove method removes a DataRow from a DataTable where as DataRowCollection.Delete method marks a row for deletion.


Calling Remove is the same as calling Delete and then calling AcceptChanges.
You can also use the Clear method to remove all members of the collection at one time.

 5. What are Extension Methods?

Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type.



6. What is the use of System.Threading.ReaderWriterLockSlim Class?

Use ReaderWriterLockSlim to protect a resource that is read by multiple threads and written to by one thread at a time. ReaderWriterLockSlim allows multiple threads to be in read mode, allows one thread to be in write mode with exclusive ownership of the lock, and allows one thread that has read access to be in upgradeable read mode, from which the thread can upgrade to write mode without having to relinquish its read access to the resource.

Wednesday, January 19, 2011

Compression in .Net

using System.IO;
using System.IO.Compression;

static void Main(string[] args)
{
CompressFile(@"d:\ShalvinSmall.jpg", @"d:\S.jpg.zip");
DecompressFile(@"d:\s.jpg.zip", @"d:\Aju.jpg");
System.Console.ReadLine();
}

static void CompressFile(string inFilename,
                     string outFilename)
{

FileStream sourceFile = File.OpenRead(inFilename);
FileStream destFile = File.Create(outFilename);

// Create the Compressed stream
GZipStream compStream =
new GZipStream(destFile, CompressionMode.Compress);

// Write the data
int theByte = sourceFile.ReadByte();
while (theByte != -1)
{
compStream.WriteByte((byte)theByte);
theByte = sourceFile.ReadByte();
}

// Clean it up
sourceFile.Close();
compStream.Close();
destFile.Close();
}

static void DecompressFile(string inFilename, string outFilename)
{
FileStream sourceFile = File.OpenRead(inFilename);
FileStream destFile = File.Create(outFilename);

// Create the Compressed stream
GZipStream compStream =
new GZipStream(sourceFile, CompressionMode.Decompress);

// Write the data
int theByte = compStream.ReadByte();
while (theByte != -1)
{
destFile.WriteByte((byte)theByte);
theByte = compStream.ReadByte();
}

// Clean it up
sourceFile.Close();
compStream.Close();
destFile.Close();
}

Friday, January 14, 2011

jQuery with Visual Studio 2010

Applying CSS based on click event
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="5JQueryCSS.aspx.cs" Inherits="_5JQueryCSS" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>

</head> 
    <title></title>
    <script src="Scripts/jquery-1.12.1.js"></script>
    <script>
        $("document").ready(function()
        {
            $("#btnHighlight").click(function ()
            {
                $("p").css("border", "3px solid red");
                return false;
            });
         
        });
        </script>
    <form id="form1" runat="server">
    <div>
    
        <p>Shalvin</p>
        <p>Biju</p>
        <p>Reny</p>

        <h3>JavaScript Technologies</h3>
        <ul>
            <li>JQuery</li>
            <li>AngularJS</li>
            <li>CommonJS</li>
            <li>NodeJS</li>
        </ul>
    </div>
        <asp:Button ID="btnHighlight"  runat="server" Text="Highlight" />
    </form>
</body>
</html>

Tuesday, January 11, 2011

Symmetric Encryption in .Net

using System.Security.Cryptography;
using System.IO;
using System.Diagnostics;


       Rfc2898DeriveBytes passwordKey;
        RijndaelManaged alg;
        FileStream inFile;
        ICryptoTransform encryptor;
        FileStream outFile;
        CryptoStream encryptStream;

        string inFileName;
        string outFileName;
        string password;
        byte[] saltValueBytes;
private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            saltValueBytes = Encoding.ASCII.GetBytes("ShalvinPD");
        }
        private void btnEncrypt_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                EncPrepare();

                // Read the unencrypted file into fileData
                inFile = new FileStream(inFileName, FileMode.Open, FileAccess.Read);
                byte[] fileData = new byte[inFile.Length];
                inFile.Read(fileData, 0, (int)inFile.Length);


                encryptor = alg.CreateEncryptor();
                outFile = new FileStream(outFileName, FileMode.OpenOrCreate, FileAccess.Write);
                encryptStream = new CryptoStream(outFile, encryptor, CryptoStreamMode.Write);

                // Write the contents to the CryptoStream
                encryptStream.Write(fileData, 0, fileData.Length);

                // Close the file handles
                encryptStream.Close();
                inFile.Close();
                outFile.Close();
                MessageBox.Show("File encrypted successfully");
                Process.Start("notepad", txtDestination.Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }

      

        private void btnDecrypt_Click(object sender, RoutedEventArgs e)
        {
            EncPrepare();
 
            // Read the encrypted file into fileData
            ICryptoTransform decryptor = alg.CreateDecryptor();
            inFile = new FileStream(inFileName, FileMode.Open, FileAccess.Read);
            CryptoStream decryptStream = new CryptoStream(inFile, decryptor, CryptoStreamMode.Read);
            byte[] fileData = new byte[inFile.Length];
            decryptStream.Read(fileData, 0, (int)inFile.Length);

            // Write the contents of the unencrypted file
            outFile = new FileStream(outFileName, FileMode.OpenOrCreate, FileAccess.Write);
            outFile.Write(fileData, 0, fileData.Length);

            // Close the file handles
            decryptStream.Close();
            inFile.Close();
            outFile.Close();
            MessageBox.Show("File encrypted successfully");
        }

        private void EncPrepare()
        {
            inFileName = txtSource.Text;
            outFileName = txtDestination.Text;
            password = txtKey.Text;
            passwordKey = new Rfc2898DeriveBytes(password, saltValueBytes);


            alg = new RijndaelManaged();
            alg.Key = passwordKey.GetBytes(alg.KeySize / 8);
            alg.IV = passwordKey.GetBytes(alg.BlockSize / 8);

          
        }
    }

WPF Xaml


<Window x:Class="WpfEncryptionShalvin.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Shalvin Encryption" Height="350" Width="525" Loaded="Window_Loaded">
    <Grid>
        <Label Content="Source File Name" Height="28" HorizontalAlignment="Left" Margin="62,42,0,0" Name="label1" VerticalAlignment="Top" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="221,42,0,0" Name="txtSource" VerticalAlignment="Top" Width="120" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="221,135,0,0" Name="txtDestination" VerticalAlignment="Top" Width="120" />
        <Label Content="Destination File Name" Height="28" HorizontalAlignment="Left" Margin="62,130,0,0" Name="label2" VerticalAlignment="Top" />
        <Label Content="Key" Height="28" HorizontalAlignment="Left" Margin="62,88,0,0" Name="label3" VerticalAlignment="Top" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="221,88,0,0" Name="txtKey" VerticalAlignment="Top" Width="120" />
        <Button Content="Encrypt" Height="23" HorizontalAlignment="Left" Margin="62,202,0,0" Name="btnEncrypt" VerticalAlignment="Top" Width="75" Click="btnEncrypt_Click" />
        <Button Content="Decrypt" Height="23" HorizontalAlignment="Left" Margin="221,202,0,0" Name="btnDecrypt" VerticalAlignment="Top" Width="75" Click="btnDecrypt_Click" />
    </Grid>
</Window>


Hashing

using System.Security.Cryptography;

private void btnHash_Click(object sender, EventArgs e)
{
    HashAlgorithm hashA = new SHA1CryptoServiceProvider();
byte[] pwordData = Encoding.Default.GetBytes(txtPlainText.Text);
byte[] hash = hashA.ComputeHash(pwordData);
txtHashCode.Text = BitConverter.ToString(hash);
}