Computer Programming 1
Jan 12, 2016
Computer Programming 1
Method variable in C# MinValue
• Display minimum value of variable in C#
MaxValue• Display maximum value of variable in C#
using System;namespace MaxMinVariable{
public class Program{
static void Main(string[] args){ Console.WriteLine(“min sbyte =” + SByte.MinValue); Console.WriteLine(“max sbyte =” + SByte.MaxValue); Console.WriteLine(“min byte =” + Byte.MinValue); Console.WriteLine(“max byte =” + Byte.MaxValue); Console.WriteLine(“min shot =” + Int16.MinValue); Console.WriteLine(“max shot =” + Int16.MaxValue); Console.WriteLine(“min ushot =” + UInt16.MinValue); Console.WriteLine(“max ushot =” + UInt16.MaxValue); Console.WriteLine(“min int =” + Int32.MinValue); Console.WriteLine(“max int =” + Int32.MaxValue); Console.WriteLine(“min uint =” + UInt32.MinValue); Console.WriteLine(“max uint =” + UInt32.MaxValue); Console.WriteLine(“min long=” + Int64.MinValue); Console.WriteLine(“max long =” + Int64.MaxValue); Console.WriteLine(“min ulong =” + UInt64.MinValue); Console.WriteLine(“max ulong =” + UInt64.MaxValue); Console.Read();}
}}
English name and meaning First name is character and then is character or member, or
insert underscore above it. ex. Ant, A1200, fName, Last_name, etc.
Not space in variables Not Reserved Word
Nor Special character # * % / ? < > ! @ $ ^ & ( ) + ‘ “
Example of integer & floating point variableusing System;namespace ExVariable{
public class Program{
static void Main(string[] args){ Console.WriteLine(20); Console.WriteLine(020); Console.WriteLine(0x20); Console.WriteLine(12345678901234”); Console.WriteLine(12345678901234L”); Console.WriteLine(3.1415926535897896f); Console.WriteLine(3.1415926535897896d); Console.Read();}
}}
Result2016321942892530123456789012343.14159273.1415926535897896
True False
Example of Boolean variableusing System;namespace ExVariable{
public class Program{
static void Main(string[] args){Console.WriteLine(true);
Console.WriteLine(false); Console.WriteLine(1 < 2); Console.WriteLine('a' == 'b');
Console.Read();}
}}
Resulttruefalsetruefalse
Char char sex = ‘M’
Example of character variableusing System;namespace ExCharVariable{
public class Program{
static void Main(string[] args){
Console.WriteLine("new line [{0}]", '\n'); Console.WriteLine("tab [{0}]", '\t'); Console.WriteLine("single quote [{0}]", '\'');
Console.Read();}
}}
Resultnew line []tab [ ]single quote [']
Some characters within a given font are not printable in the sense that you don’t see anything when you look at them on the computer screen or printer. The most obvious example of this is the space, which is represented by the
character ‘ ‘ (single quote, space, single quote).
escaped sequence of Unicode character if you write\u follow hexadecimal number 4 digit.
Example of escaped sequence variableusing System;namespace ExCharVariable{
public class Program{
static void Main(string[] args){
System.Console.WriteLine('\u0061'); System.Console.WriteLine("\u0048\u0065\u006c\u006c\u006f"); System.Console.WriteLine('\u0e01');
Console.Read();}
}}
ResultaHelloก
Another common variable type is the string. The following examples show how you declare and initialize
string variables:
// declare now, initialize laterstring someString1;someString1 = “this is a string”;// or initialize when declaredstring someString2 = “this is a string”;string someString = “This is a line\nand so is this”;
string s = “this is a phrase”+ “ and so is this”;
string s1 = “a”; string s2 = “b”; string s3 = s1 + s2; // result is “ab”
Humans don’t treat different types of counting numbers differently. For example, a normal person (as distinguished from a
C# programmer) doesn’t think about the number 1 as being signed, unsigned, short, or long. Although C# considers these
types to be different, even C# realizes that a relationship exists between them. For example, the following code converts an int into
a long:
// this is OK // this is illegalint nValue = 10; long lValue = 10;long lValue; int nValue;lValue = nValue; nValue = lValue;
But what if you know that the conversion is okay? // this is now OKlong lValue = 10; double dValue = 10.0;int nValue; long lValue = (long)
dValue;nValue = (int) lValue;
sbyte shot, int, long, float,double, decimal byte shot, ushot, int, uint, long, ulong, float,double,
decimal shot int, long, float,double, decimal ushot int, uint, long, ulong, float,double, decimal int long, float,double, decimal uint long, ulong, float,double, decimal long loat,double, decimal char ushot, int, uint, long, ulong, float,double, decimal float double
exampleint n1 = 10 ;double d2 = 5.0 ;int nResult = n1 * (int) d2 ;
C# don’t direct a numeric to input method . In this last chapter C# have String input method , so C# to design
method Parse for to change variable in .NET Framework . Syntax : <Numeric data type(.NET)> . Parse(<string
expressing>).NET Numeric
DecimalDoubleSingleInt16Int32Int64
UInt16UInt32UInt64ByteSByte
C# Namedecimaldoublefloatshortint
longushortuint
ulongbytesbyte
C# Namedecimaldoublefloatshortint
longushortuint
ulongbytesbyte
Exampleusing System;namespace ExChgNumStr{
public class Program{
static void Main(string[] args){ double num1, num2, sum;
Console.Write(“Enter Num 1 : ”); num1 = Double.Parse(Console.ReadLine());
Console.Write(“Enter Num 2 : ”); num2 = Double.Parse(Console.ReadLine());
sum = num1 + num2;
Console.WriteLine(“{0} + {1} = {2}”,num1,num2,sum); Console.Read();}
}}
ResultEnter Num 1 : 3.5 Enter Num 2 : 2.43.5 + 2.4 = 5.9
In display output to C# on Console or Windows application will display output to String variable. So that if the data to display output is numeric and wanted to show in display,
you will be to convert from numeric to String.
This method is ToString in class Convert
Syntax:• Convert. ToString(<string>);
Example:num = 2 + 3;
str1 = Convert. ToString(num);
Exampleusing System;namespace ExChgStrNum{
public class Program{
static void Main(string[] args){ int num1, num2, sum; string str1;
num1 = 35; num2 = 25; sum = num1 + num2;
str1 = Convert. ToString(sum); Console.WriteLine(“num1 + num2 = ”); Console.WriteLine(str1);
Console.Read();}
}}
Resultnum1 + num2 = 60