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

Building a Responsive Weather App with React

 Fetching and Displaying Weather Data      By - ABDUL YESDANI Objective:  This document will help you prepare for the workshop on building a responsive weather app using React. You will learn the basics of React, how to use APIs, and how to work with data fetched from an API. By the end of the workshop, you’ll build a simple weather app that fetches real-time weather data.  1. What is React? React is a JavaScript library used to build user interfaces, especially for single-page applications. It lets you create components, which are reusable, isolated pieces of code that define a part of your user interface. Key Terms Component : Think of components as building blocks for your app. Each part of your app’s UI can be a component, like a button, form, or navigation bar. JSX : A syntax extension for JavaScript, which looks a lot like HTML. JSX is used to describe what the UI should look like. State : Data that controls what gets rendered on the screen. State can change over time, like inpu

.Net Presentation

.NET is a free, cross-platform, open source developer platform for building many different types of applications. With .NET, you can use multiple languages, editors, and libraries to build for web, mobile, desktop, games, and IoT. You can write .NET apps in C#, F#, or Visual Basic. C# is a simple, modern, object-oriented, and type-safe programming language. F# is a programming language that makes it easy to write succinct, robust, and performant code. Visual Basic is an approachable language with a simple syntax for building type-safe, object-oriented apps. You can write .NET apps in C#, F#, or Visual Basic. C# is a simple, modern, object-oriented, and type-safe programming language. F# is a programming language that makes it easy to write succinct, robust, and performant code. Visual Basic is an approachable language with a simple syntax for building type-safe, object-oriented apps.

node.js tutorial

node.js is a open source server environment. Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine. Node.js is free,it is cross platform means runs on various platforms (Windows, Linux, Unix, Mac). Node.js uses JavaScript on the server.