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()
{
}

(1)The Anatomy Of a Simple C# Program

In C# it is not possible to create global functions or global points of data.Rather,all data members and methods must be contained within a type definition.

First make a Console Application Project in C#.
//**********************************************//
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
}
}
}
//**********************************************//
It will make a project as above.


You can update your Main() by adding some additional code in you Main() body
//**************************************************//
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//Dispaly a simple Message to the User
Console.WriteLine("Hello World");
Console.WriteLine("C# World");

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

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

Formally speaking,the class that defines the Main() method is termed the "application object"

While it is possible for a single executable application to have more than one application object.

For the time being,simply understand that static members are scoped to the class level(rather than the object level) and can thus be created without the need to first create a new class instance.

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

The keyword static allows Main( ) to be called before an object of its class has been created. This is necessary because Main( ) is called at program startup. The keyword void simply tells the compiler that Main( ) does not return a value. As you will see, methods can also return values.

Monday, April 6, 2009

C# InterView Question

I am going yo put here some interview questions of C# which will help you in near future.

C# Open Discussion

In this topic you can discuss all the problems related to C#.The Experts will try to answer you queries as soon as possible.

Note:
Please mention you name and e-mail address.Our experts will mail you if required.

Core C# Programming Constructs

In this part you will learn the basic concepts of C# Language.
This part include the following topics...
  1. The Anatomy of a Simple C# Program
  2. Some Additional Members of the System.Environment Class
  3. The System.Console Class
  4. System Data Types and C# Shorthand Notation
  5. Understanding the System.String Type
  6. Narrowing and Widening Data Type Conversions
  7. C# Iteration Constructs
  8. Decision Constructs and the Relational/Equity Operators