Search Results

Saturday, April 11, 2009

Understanding the System.String Type and String Functions

System.String provides a number of methods you would expect from such a utility class, including methods that return the length of the character data, find sub-strings within the current string,convert to and from uppercase/lowercase, and so forth.

The String Type resides in the System namespace.All the string objects are mapped on the managed heap.

Below is some String Functions Listed...
//*****************************************************************************//
Length This property returns the length of the current string.

Compare() This method compares two strings.

Contains() This method determines whether a string contains a specific substring.

Equals() This method tests whether two string objects contain identical character data.

Format() This method formats a string using other primitives (e.g., numerical data, other
strings) and the {0} notation examined earlier in this chapter.
Insert() This method inserts a string within a given string.

PadLeft() This method is used to pad a string with some characters.

PadRight()This method is used to pad a string with some characters.

Remove() Use these methods to receive a copy of a string, with modifications (characters)

Replace() removed or replaced).

Split() This method returns a String array containing the substrings in this instance that are delimited by elements of a specified Char or String array.

Trim() This method removes all occurrences of a set of specified characters from the beginning and end of the current string.

ToUpper() These methods create a copy of the current string in uppercase or lowercase

ToLower() format, respectively.
//*****************************************************************************//

Parsing Values from String Data

This "Parsing" technique can be extremely helpful when you wish to convert a bit of user input data (such as a selection from a GUI-based drop-down list box) into a numerical value.

Consider the following parsing logic within a method named ParseFromStrings():
//***********************************************************************//
static void ParseFromStrings()
{
Console.WriteLine("=> Data type parsing:");
bool b = bool.Parse("True");
Console.WriteLine("Value of b: {0}", b);
double d = double.Parse("99.884");
Console.WriteLine("Value of d: {0}", d);
int i = int.Parse("8");
Console.WriteLine("Value of i: {0}", i);
char c = Char.Parse("w");
Console.WriteLine("Value of c: {0}", c);
Console.WriteLine();
}
//***********************************************************************//
OutPut:

Value of b:True
Value of d:99.84
Value of i:8
Value of c:w

NOTE:


The important thing to understand in this is Parse function becomes very important when you are working on GUI applications in which you have to take input from user in string format e.g.;

Enter Your Age : 21

Enter Date: 11/04/2009

In these scenarios to convert each data type to its actual type we use the method on Parsing,which is available for every data type(ValueType) present in C#.

Intrinsic Data Types


All intrinsic data types support what is known as a default constructor.
This feature allows you to create a variable using the new keyword, which automatically sets the variable to its default value:

  • bool types are set to false.
  • Numeric data is set to 0 (or 0.0 in the case of floating-point data types).
  • char types are set to a single empty character.
  • DateTime types are set to 1/1/0001 12:00:00 AM.
  • Object references (including strings) are set to null.

The following is syntactically well-formed C# code:
//************************************************************************//
static void NewingDataTypes()

{
Console.WriteLine("=> Using new to create intrinsic data types:");
bool b = new bool(); // Set to false.
int i = new int(); // Set to 0.
double d = new double(); // Set to 0.
DateTime dt = new DateTime(); // Set to 1/1/0001 12:00:00 AM
Console.WriteLine("{0}, {1}, {2}, {3}", b, i, d, dt);
Console.WriteLine();
}
//************************************************************************//

Thursday, April 9, 2009

System Data Types

The Table below are the C# data type keywords which are present in System Namespace.

***************************************************************************************
Meaning in Life=
***************************************************************************************
C#Shorthand= bool
System Type=System.Boolean
Meaning in Life=Represents truth or falsity

C#Shorthand=sbyte
System Type=System.SByte
Meaning in Life=Signed 8-bit number

C#Shorthand=byte
System Type=System.Byte
Meaning in Life=Unsigned 8-bit number

C#Shorthand=short
System Type=System.Int16
Meaning in Life=Signed 16-bit number

C#Shorthand=ushort
System Type=System.UInt16
Meaning in Life=Unsigned 16-bit number

C#Shorthand=int
System Type=System.Int32
Meaning in Life=Signed 32-bit number

C#Shorthand=uint
System Type=System.UInt32
Meaning in Life=Unsigned 32-bit number

C#Shorthand=long
System Type=System.Int64
Meaning in Life=Signed 64-bit number

C#Shorthand=ulong
System Type=System.UInt64
Meaning in Life=Unsigned 64-bit number

C#Shorthand=char
System Type=System.Char
Meaning in Life=Single 16-bit Unicode character

C#Shorthand=float
System Type=System.Single
Meaning in Life=32-bit floating-point number

C#Shorthand=double
System Type=System.Double
Meaning in Life=64-bit floating-point number

C#Shorthand=decimal
System Type=System.Decimal
Meaning in Life=96-bit signed number

C#Shorthand=string
System Type=System.String
Meaning in Life=Represents a set of Unicode characters

C#Shorthand=object
System Type=System.Object
Meaning in Life=The base class of all types in the .NET universe
***************************************************************************************

NOTE:

  • Each of the numerical types (short, int, and so forth) map to a corresponding structure in the System namespace.
  • In a nutshell, structures are "value types" allocated on the stack.
  • On the other hand, string and object are "reference types," meaning the variable is allocated on the managed heap.
  • Simply understand that value types can be allocated into memory very quickly and have a very fixed and predictable lifetime.

Wednesday, April 8, 2009

Basic Input/Output With the Console Class

Here i have written a program that takes name and age as an input and displays it on the Console by changing the color of the text...
//***********************************************************************************//
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("******Basic console I/O********");
GetUserData();
Console.ReadLine();

}
static void GetUserData()
{
//Get Name and Age
Console.Write("Please Enter Your Name");
string userName = Console.ReadLine();

Console.Write("Please Enter Your Age");
string userAge = Console.ReadLine();

//Change echo color,just for fun
ConsoleColor prevColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Green;

//Echo to the Console
Console.WriteLine("Hello {0} You are {1} years old.",userName,userAge);

//Restore Previous Color
Console.ForegroundColor = prevColor;
}
}
}
//*******************************************************************************//
InPut:
Enter Your Name Faisal
Enter Your Age
OutPut:
Hello Faisal You are 21 Years Old.(color will be changed to green)


NOTE:


Here in this line one thing to notice is {0} and {1}...

The question is why we are using it in Console.ReadLine()??

The reason we are using is this that Console.ReadLine( ) works just like an array of strings.whatever you input first goes to the first index of Console.ReadLine() and so on...

Or we can say that all the inputs given from the Console is saved in a temporary array of strings,
which we used in Console.WriteLine()

Remember that its index always start from 0,1,2,....


You can also repeat the index {0} , {0} in Console.WriteLine

Console.WriteLine("{0},Number {0},Number{0}",9")

(2)An interesting Aside:Some Additional Members of the System.Enviroment Class

This class allows you to obtain a number of details regarding the operating system currently hosting your .Net Application using various static members.

//**************************************************************//
The program below is an Example Of Enviroment Class...
//**************************************************************//
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{

ShowEnviromentDetails();
//wait for enter key to be presses before quit
Console.ReadLine();

}
static void ShowEnviromentDetails()
{
//Print Out the drives on this machine and other interesting details

foreach (string drive in Environment.GetLogicalDrives())
Console.WriteLine("Drive:{0}", drive);

Console.WriteLine("OS:{0}", Environment.OSVersion);
Console.WriteLine("Number of processors:{0}",Environment.ProcessorCount);
Console.WriteLine(".Net Version:{0}",Environment.Version);

}
}
}
//**************************************************************//

//**************************************//
Select Properties of System.Enviroment
//**************************************//

**********************************
Property and its Meaning in Life
**********************************

Exit Code
Gets or sets the exit code any where within the application

Machine Name
Gets the name of the current machine

NewLine
Gets the new line symbol for the current enviroment

StackTrace
Gets the current stack trace information for the application

System Directory
Returns the full path to the system directory

User Name
Returns the name of user that started this application

//*********************************************************************************//

You can use more Enviroment variables by using the intellisence...just like this

Console.WriteLine(Environment.(Here a list of properties will appear in front of you) );

Do Check this for your practice...

Variations On the Main() Method

//******************************************//
Three are three ways to define Main() in C#
//******************************************//

//int return type,array of strings as the argument
static int Main( string[] args)
{
}

//No return type,No argument
static void Main( )
{
}

//int return type,no argument
static int Main()
{
}