CSharp supports converting String to Int in many ways. The following are the few examples to do that.

Example 1:

Int32 intValue = Convert.ToInt32("String Value");

Here the String Value can be 10 or 100 or any number value. This Convert Class contains many methods which, can convert a String to any other datatype which .Net supports

 

Following are the few for your idea.

 

DateTime dateValue = Convert.ToDateTime("String Value");

Double doubleValue = Convert.ToDouble("String Value");

 

I have copied this table from MSDN which could help you to understand this more.

 

Name Description
ChangeType Overloaded. Returns an object of a specified type whose value is equivalent to a specified object.
FromBase64CharArray Converts a subset of a Unicode character array, which encodes binary data as base-64 digits, to an equivalent 8-bit unsigned integer array. Parameters specify the subset in the input array and the number of elements to convert.
FromBase64String Converts the specified string, which encodes binary data as base-64 digits, to an equivalent 8-bit unsigned integer array.
GetTypeCode Returns the TypeCode for the specified object.
IsDBNull Returns an indication whether the specified object is of type DBNull.
ToBase64CharArray Overloaded. Converts a subset of an 8-bit unsigned integer array to an equivalent subset of a Unicode character array encoded with base-64 digits.
ToBase64String Overloaded. Converts the value of an array of 8-bit unsigned integers to its equivalent string representation that is encoded with base-64 digits.
ToBoolean Overloaded. Converts a specified value to an equivalent Boolean value.
ToByte Overloaded. Converts a specified value to an 8-bit unsigned integer.
ToChar Overloaded. Converts a specified value to a Unicode character.
ToDateTime Overloaded. Converts a specified value to a DateTime value.
ToDecimal Overloaded. Converts a specified value to a decimal number.
ToDouble Overloaded. Converts a specified value to a double-precision floating-point number.
ToInt16 Overloaded. Converts a specified value to a 16-bit signed integer.
ToInt32 Overloaded. Converts a specified value to a 32-bit signed integer.
ToInt64 Overloaded. Converts a specified value to a 64-bit signed integer.
ToSByte Overloaded. Converts a specified value to an 8-bit signed integer.
ToSingle Overloaded. Converts a specified value to a single-precision floating-point number.
ToString Overloaded. Converts the specified value to its equivalent string representation.
ToUInt16 Overloaded. Converts a specified value to a 16-bit unsigned integer.
ToUInt32 Overloaded. Converts a specified value to a 32-bit unsigned integer.
ToUInt64 Overloaded. Converts a specified value to a 64-bit unsigned integer.

 

 

The another way is using Parse. Most of the datatype might have this Parse method.

 

Example 2:

 

Int32 x = Int32.Parse("10000");


You can explore this by typing the datatype and place a dot(.) next to the type.  Check the below for more clarity.

 

Double.Parse

DateTime.Parse

 

You can read boxing and unboxing after this.

 

 



» File Open Dialog box in CSharp

» LINQ - Very Basic Queries in Csharp


Similar articles

» File Open Dialog box in CSharp

File Open Dialog box in C#

To Open or to Save file we use dialog box in all the windows applications. To use open or save dialog box we should use the following directive statement.

using System.Windows.Forms;

Because these dialog boxes are defined in System.windows.Forms.

In the following code i have used the file open dialog box for demo.

OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Title = "Kanbal.com open dialog box demo";
openFileDialog.InitialDirectory =
@"c:\Program Files";
openFileDialog.Filter =
"All files (*.*)|*.*|All files (*.*)|*.*";
openFileDialog.FilterIndex = 2;
openFileDialog.RestoreDirectory =
true;

if (openFileDialog.ShowDialog() == DialogResult.OK)
{
txtFileName.Text = openFileDialog.FileName;
}

The above code will display the open dialog box with all files filter like exactly below.

open-dialog-box


You can use various properties like Title, Filter, InitialDirectory to make your dialog box more friendly.



» LINQ - Very Basic Queries in Csharp

LINQ - Language Integrated Query

LINQ enable us to query the collection objects to get the result based on the condition. The follwoing example will give you an idea about a basic LINQ query.

For example,
List<Person> PersonList = new List<Person>(){
new Person(){Name = "Raja", Age = 25},
new Person(){Name = "Kannan", Age = 28},
new Person(){Name = "Vijay", Age = 27},
new Person(){Name = "Ashok", Age = 30}
};

The above code will create a list of type Person with 4 items in it.

Consider a situation where you need to filter the items who is have age on or below 28.
This scenario can be filtered using the following query.

var query = from p in PersonList
where p.Age <= 28
select p;

The above query will read each items from PersonList into the local variable p, and checks whether p.Age <=28. If the condition is satisfied, that item 'p' will be added in to result.

List<Person> resultList = query.ToList();

'var' is new data type which can hold any type of data. In our example, it holds the LINQ query. But, the query will not hold any result of that query. The result of the query can be taken by converting that 'query' into List using ToList(), or it can be taken which we iterate the query using 'foreach'. Until that, it will just hold the query.


To sort the list by age in ascending order.
var query = from p in PersonList
order p by p.Age asc
select p;

To collect only Age field
List<Int32> ageList = (from p in PersonList
select p.Age).ToList();