Skip to main content

Learn C# in Easy Way, Step-4 :Type conversion with strings, numbers.

 Hi, Welcome to all. My name is Abdul Yesdani. Today I am going to teach you how to convert string to number and vice versa. Conversion is two types : Implicit type conversion and Explicit type conversion.

What is implicit type conversion in C#?

C# compiler automatically converts from one type to other type is called implicit conversion.

Example: int items =3;

                string person ="Kamat";

When we mix these two variables and print them see what we get.

         Console.WriteLine( name + "sold " + items + " Products);

 Output will be : Kamat sold 3 Products.

In the above statement  'items' is a numeric variable, which is automatically converted to string.


What is explicit conversion in C#?

We need to force convert from one type to other type. Observe the following statements. I want to add 7 to the items.

 Console.WriteLine( name + "sold " + items + 7+ " Products");

When we run it we get output : Kamat sold 37 Products. Here 7 concatenated to the string not added.

To add it we need to specifically tell the compiler like below.

 Console.WriteLine( name + "sold " +( items + 7)+ " Products");

Now you will get output : Kamat sold 10 Products, Here 3 and 7 get added.

To convert from one type to other in C# we use Convert class methods. To convert from string to int here we used Convert.ToInt32().

Example:



The below shown the output for the above code.




watch the video 





Comments

Popular posts from this blog

SQL server

  Microsoft SQL Server: Microsoft SQL Server is a relational database management system (RDBMS). Applications and tools connect to a SQL Server instance or database, and communicate using Transact-SQL (T-SQL). You can install SQL Server on Windows or Linux, deploy it in a Linux container, or deploy it on an Azure Virtual Machine. SQL Server components and technologies: Database Engine The Database Engine is the core service for storing, processing, and securing data. The Database Engine provides controlled access and transaction processing to meet the requirements of the most demanding data consuming applications within your enterprise.  Integration Services SQL Server Integration Services (SSIS) is a platform for building high performance data integration solutions, including packages that provide extract, transform, and load (ETL) processing for data warehousing. Analysis Services SQL Server Analysis Services (SSAS) is an analytical data platform and toolset for personal, te...

C# Tutorial, Arrays

Hi, Welcome to all. My name is Abdul Yesdani. Today we are going to learn about arrays in C#. In our previous postings we have worked with variables. One variable can store only one value. If we want to store multiple values of same type, array variable is there to do that job. What is an array? An array variables is capable of storing multiple values of same type.   Syntax:  datatype[ ] arrayName = new datatype[size]; Example: int [ ] myNumbers = new int [ 5];  The above array variable  myNumbers is capable of storing 5 integer values. In the memory it will allocate 5 slots to store 5 values, index starting from 0 to 4. Array indexing will always start with 0 and end with size -1. Types of Arrays in C# : There are 3 types of array in C#.  Single dimensional array, multi dimensional array and jagged array. Single dimension array represent a single column or row of values. The above example is for the same. Multi dimension array represent 2 dimension or 3 di...