Visual C#.NET: Windows Forms Programming with C# by mohmmed moaml & Beerbohm Max
Author:mohmmed, moaml & Beerbohm, Max [mohmmed, moaml]
Language: eng
Format: epub
Published: 2019-11-12T16:00:00+00:00
A simple program to add two integers
Let us now work on a more practical program. We will write a program that combines two integers and shows the result to the user. Follow the same steps that we performed in the previous program to create a new program named SumTwoNumbers, copy the following code contents to the Program.cs file:
using System;
namespace SumTwoNumbers
{
class Program
{
static void Main (string [] args)
{
int a;
int b;
int c;
a = 3;
b = 4;
c = a + b;
Console.WriteLine ("3 plus 4 equals:" + c.ToString ());
}
}
}
This simple program introduces the concept of declaring and dealing with variables. In lines 9 through 11 we have declared three variables a, b, and c of type int.
Each variable must be declared in C Sharp before using it in the program. Note that a variable is declared by its type and hence its name. The int variable can accommodate any integer (without a decimal point) between -2147,483,648 and 2,147,483,647. Note that we have assigned values 3 and 4 to variables a and b respectively, in lines 13 and 14. We are adding and assigning the variable c on line 16. On line 18, we display a message to the user.
A variable can be assigned a value directly when declared. For example, values 3 and 4 can be assigned to variables a and b respectively, when declared:
int a = 3;
int b = 4;
A single statement statement can also be used to declare several variables at the same time. For example, the previous variables a, b and c could be declared in one software statement as follows:
int a, b, c;
In fact, there are two types of variables supported by DotNet. Value types and reference types. We'll talk about them later.
The int type is a value type. At least one assignment to a value variable must be performed before reading it. Otherwise we will get an error. Try deleting the code statement on line 16 responsible for assigning the value of the sum to the variable c. Run the program and you will get the following error:
Use of unassigned local variable 'c'
This is because we tried to read variable c in line 18 without assigning any value to it.
The code statement on line 18 is responsible for displaying the message to the user as mentioned above. You'll notice that we've passed the following expression to the WriteLine function:
"3 plus 4 equals:" + c.ToString ()
A programmatic expression is a concept found in almost all programming languages, and is simply the result of a software operation using one or more operator operators, or a call to a function or a combination between them. We call expression processing by expression evaluation. The factor used here is the combining factor (+), and you may be surprised why I call it the combining factor, although it is similar to the normal addition factor that combines two numbers together (see line 16). This is due to the type of operands on both ends. You can easily notice that the left operand is the text:
"3 plus 4 equals:"
The right coefficient is:
c.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Implementing Enterprise Observability for Success by Manisha Agrawal and Karun Krishnannair(7353)
Supercharging Productivity with Trello by Brittany Joiner(6617)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6420)
Mastering Tableau 2023 - Fourth Edition by Marleen Meier(6380)
Inkscape by Example by István Szép(6230)
Visualize Complex Processes with Microsoft Visio by David J Parker & Šenaj Lelić(5929)
Build Stunning Real-time VFX with Unreal Engine 5 by Hrishikesh Andurlekar(4923)
Design Made Easy with Inkscape by Christopher Rogers(4608)
Customizing Microsoft Teams by Gopi Kondameda(4147)
Linux Device Driver Development Cookbook by Rodolfo Giometti(3935)
Extending Microsoft Power Apps with Power Apps Component Framework by Danish Naglekar(3737)
Business Intelligence Career Master Plan by Eduardo Chavez & Danny Moncada(3704)
Salesforce Platform Enterprise Architecture - Fourth Edition by Andrew Fawcett(3613)
Pandas Cookbook by Theodore Petrou(3589)
The Tableau Workshop by Sumit Gupta Sylvester Pinto Shweta Sankhe-Savale JC Gillet and Kenneth Michael Cherven(3393)
TCP IP by Todd Lammle(2987)
Drawing Shortcuts: Developing Quick Drawing Skills Using Today's Technology by Leggitt Jim(2915)
Applied Predictive Modeling by Max Kuhn & Kjell Johnson(2874)
Exploring Microsoft Excel's Hidden Treasures by David Ringstrom(2845)
