C#.NET is my world

My learnings of ASP.NET

  • Archives

  • Make your plan :)

    May 2012
    M T W T F S S
    « Oct    
     123456
    78910111213
    14151617181920
    21222324252627
    28293031  

C# code to write data into text file and to read from text file

Posted by techymeg on October 12, 2010

The below code is useful to write data into text file and to read from the text file.

using System;

using System.IO;

namespace LearnDataTypes

{   

    class Program

    {

        static void Main(string[] args)

        {

            //Writing into file using C# Code

            StreamWriter sw = new StreamWriter("C:\\Notes.txt");

            sw.WriteLine("Hi, I am writing from CSharp code into file");

            sw.Close();

 

            //Reading from a file using C# Code

            StreamReader sr = new StreamReader("C:\\Notes.txt");

            Console.WriteLine(sr.ReadToEnd());

            sr.Close();

 

            //What about the below code :)

            //while(!sr.EndOfStream)

            //{

            //    Console.Write(Convert.ToChar(sr.Read()));

            //}

            Console.ReadLine();

        }

    }

}

Hope this will be useful.

Happy Coding.

Cheers,

Meganadha Reddy K.

Posted in CSharp Language | Leave a Comment »

Difference between Value type and Reference type

Posted by techymeg on October 12, 2010

Hi All,
Please find below the pictorial difference between value type and reference type:
Value Type - View
Reference Type

Hope this will be useful.
Happy coding.

Cheers,
Meganadha Reddy K.

Posted in CSharp Language | Leave a Comment »

Differences between Class and Structure in C#

Posted by techymeg on October 12, 2010

Differences between Class and Structure in C#:

I always wanted to know the difference between class and struct.
I am pretty clear now and hope after reading this you will also be clear.
If you find some more differences please add the same as comments.

Sl.No. Class Struct
1 Reference Type Value Type
2 Can assign null to object Cannot assign null to object
3 Copying one object to other object will copy the memory location. [Refer the below example code] Copying one object to other will just create a new copy of the existing object. [Refer the below example code]
4 Inheritance is allowed Inheritance is not allowed for structs
5 We can initialize the class variables to the value we need. We cannot initialize the struct members.

For Example:

struct Marks
{
    public int Percentage=100;           
}

** This will throw an error.

 

Class Example [for Sl. No. 3 above for Class Column]
 

using System;
namespace LearnDataTypes
{   
    class Program
    {
        class Marks
        {
            public int Percentage;           
        }
        static void Main(string[] args)
        {           
            Marks m1 = new Marks();
            m1.Percentage = 80;           
            Marks m2 = m1;
            m1.Percentage += 1;
            m2.Percentage += 2;
            Console.WriteLine(“m1.Percentage = ” + m1.Percentage +
                                    “;m2.Percentage = ” + m2.Percentage);
            Console.ReadLine();           
        }
    }
}

Output:

m1.Percentage = 83; m2.Percentage = 83

 

Struct Example [for Sl. No. 3 above for Struct Column]
using System;
namespace LearnDataTypes
{   
    class Program
    {
        struct Marks
        {
            public int Percentage;           
        }
        static void Main(string[] args)
        {           
            Marks m1 = new Marks();
            m1.Percentage = 80;           
            Marks m2 = m1;
            m1.Percentage += 1;
            m2.Percentage += 2;
            Console.WriteLine(“m1.Percentage = ” + m1.Percentage +
                                    ”;m2.Percentage = ” + m2.Percentage);
            Console.ReadLine();           
        }
    }
}
Output:

m1.Percentage = 81; m2.Percentage = 82

 

Hope this will be helpful.

Happy Coding.

Cheers,

Meganadha Reddy K.

Posted in CSharp Language | Leave a Comment »

Data Types in C# (Contd…)

Posted by techymeg on October 11, 2010

FCL Name Data Type Bytes Range Purpose
Char char 2   Unicode Character
Boolean bool 1   True/False Values
DataTime DateTime 8 1/1/0001 to 12/31/9999 To store date time

These are all value types where the value will be directly stored in stack.

Hope this will be useful.

Happy Coding.

Cheers,

Meganadha Reddy K.

Posted in CSharp Language | Leave a Comment »

Data Types in C# Language – For Storing Numerical Values

Posted by techymeg on October 11, 2010

Befor learning any programming language, it is verymuch important to know what are all the datatypes available and how efficiently we can use the same. Proper use of the datatypes will certainly improve the performance of the appliacation as well.

C# Data Types:

FCL Name Data Type Bytes Range Purpose
SByte sbyte 1 -128 to 127  
Byte byte 1 0 to 255  
Int16 short 2 -32768 to 32767  
Int32 int 4    
UInt32 uint 4    
Int64 long 8    
UInt64 ulong 8    
Single float 4   Floating point numbers
Double double 8   Large Floating point numbers
Decimal decimal 16   Numbers which need highest precision.

 

C# Console application code to check the same:

using System;
namespace LearnDataTypes
{
///
/// Program to print the size and Range of all C# Datatypes.
/// Author : Meganadha Reddy K., Date: 11-October-2010
///
class Program
{
static void Main(string[] args)
{
Console.WriteLine("sizeof({0}):{1} Bytes,\t MaxValue: {2}\tMinValue: {3}","sbyte",sizeof(sbyte),sbyte.MaxValue,sbyte.MinValue);
Console.WriteLine("sizeof({0}):{1} Bytes,\t MaxValue: {2}\tMinValue: {3}", "byte", sizeof(byte), byte.MaxValue, byte.MinValue);
Console.WriteLine("sizeof({0}):{1} Bytes,\t MaxValue: {2}\tMinValue: {3}", "short", sizeof(short), short.MaxValue, short.MinValue);
Console.WriteLine("sizeof({0}):{1} Bytes,\t MaxValue: {2}\tMinValue: {3}", "int", sizeof(int), int.MaxValue, int.MinValue);
Console.WriteLine("sizeof({0}):{1} Bytes,\t MaxValue: {2}\tMinValue: {3}", "uint", sizeof(uint), uint.MaxValue, uint.MinValue);
Console.WriteLine("sizeof({0}):{1} Bytes,\t MaxValue: {2}\tMinValue: {3}", "long", sizeof(long), long.MaxValue, long.MinValue);
Console.WriteLine("sizeof({0}):{1} Bytes,\t MaxValue: {2}\tMinValue: {3}", "ulong", sizeof(ulong), ulong.MaxValue, ulong.MinValue);
Console.WriteLine("sizeof({0}):{1} Bytes,\t MaxValue: {2}\tMinValue: {3}", "float", sizeof(float), float.MaxValue, float.MinValue);
Console.WriteLine("sizeof({0}):{1} Bytes,\t MaxValue: {2}\tMinValue: {3}", "double", sizeof(double), double.MaxValue, double.MinValue);
Console.WriteLine("sizeof({0}):{1} Bytes,\t MaxValue: {2}\tMinValue: {3}", "decimal", sizeof(decimal), decimal.MaxValue, decimal.MinValue);
Console.ReadLine();
}
}
}

Output:

C# DataTypes : Program Output

C# DataTypes : Program Output

Other available datatypes will be available in next post. :)

Hope this will be useful to someone.

Happy coding.

Cheers,

Meganadha Reddy K.

Posted in CSharp Language | Leave a Comment »

ASP.NET Login Control button event on press of Enter key

Posted by techymeg on October 11, 2010

ASP.NET Login Control button event on press of Enter key:

If we have only login control in our home page then it’s well and good that on enter key press the button click event of login control will be fired and the things will be fine for you.

If we have multiple buttons and one of them is in ASP.NET Login View [For example your page may have “Admin” button at the top right corner and Login Control in middle of page]. Now, when we press enter key after entering password in login control, “Admin” button click event will be fired (which we don’t want to happen). This article will help you how to add Login Control button to be fired one press of Enter key.

So, this article will help you in adding ASP.NET Login View button event when you press Enter Key:

Add the below code in Page_Load() of .aspx.cs page:

if (!this.IsPostBack)
{
//This code will help you to find the Id of the Button in Login Control.
Control myLoginButton = MyLogin.FindControl("LoginButton");
if (myLoginButton != null && myLoginButton is IButtonControl)
MyLogin.Attributes.Add("onKeyPress", doClick('"+ myLoginButton.ClientID+"',event)");
}

Note that, here MyLogin is the Id of your ASP.NET Login Control

Add the below code in <head> section of .aspx page:

function doClick(buttonName, e) {
var key;
if (window.event)
key = window.event.keyCode; //IE
else
key = e.which; //firefox
if (key == 13) {
var btn = document.getElementById(buttonName);
if (btn != null) { //If we find the button click it
btn.click();
event.keyCode = 0
}
}
}


The same can be used for normal button as well; this post will help you to do it for ASP.NET Login Control.

Hope this will be helpful to someone.
Happy Coding.

Cheers,
Meganadha Reddy K.

Posted in ASP.NET | 1 Comment »

C# 4.0 Langauge Specification

Posted by techymeg on October 7, 2010

Dear All,

Microsoft released the final version of .Net Framework 4.0 in April 2010.
You can Click here to download C# 4.0 Language Specification.
This is official microsoft C# 4.0 Language Specification.

Happy Learning.

Cheers,
Meganadha Reddy K.

Posted in CSharp Language | Leave a Comment »

 
Follow

Get every new post delivered to your Inbox.