Posts

Arrays in C#

If we want to store a group of similar data types, we can make use of Arrays. Write a program to save 3 even numbers in an integer array. Print the third number. using System ; class Program { static void Main ( ) { int [ ] evenNumners = new int [ 3 ] ; evenNumners [ 0 ] = 0 ; evenNumners [ 1 ] = 2 ; evenNumners [ 2 ] = 4 ; //we can also define array as below: //int[] evenNumners = { 0, 2, 4 }; Console . WriteLine ( evenNumners [ 2 ] ) ; //Output is 4 } }

Datatypes Conversion in C#

Image
Implicit conversions Explicit conversions Difference between Parse() and TryParse() Implicit conversion Implicit conversion is done by the compiler when there is no loss of information if the conversion is done If there is no possibility of throwing exceptions during the conversion Example: Write a program to convert integer to float with the use of variables. In this case no loss of information, hence no need of explicit conversion. using System ; class Program { static void Main ( ) { int a = 10 ; float f = a ; Console . WriteLine ( f ) ; //Output will be 10 } } Explicit Conversion When there is data loss situation in conversion, we need to convert that data explicitly. See below example. Example: Write a program to convert float to integer with the use of variables. using System ; class Program { static void Main ( ) { float f = 123.45 F ; int i = ( int ) f

Nullable Types in C#

Image
In C# types are divded into 2 broad categories: Value types - int, float, double, structs, enums, etc. Reference types - Interface, Class, delegates, arrays, etc. by default value types are non nullable. TO make them nullable use ? int i = 0 (i is non nullable, so i cannot be set to null, i = null will generate compiler error) int? j = 0 (j is nullable int, so j = null is legal) Nullable types bridge the difference between C# types and database types Below program shows the compiler error that we get if we try to set the null value to the value types. Write a program to check is user is married or not. Set boolean variable to null while writing the program. using System ; class Program { static void Main ( ) { bool ? isMarried = null ; if ( isMarried = = true ) Console . WriteLine ( " User is married " ) ; else if ( isMarried = = true ) Console . WriteLine ( "

Common Operators in C#

Image
Assignment operator = Arithmetic operators like +, -, *, /, % Comparison operators like ==, !=, >, >=, <, <= Conditional operators like &&, || Ternary operator ?: Null coalescing operator ?? Write a program to include all above mentioned common operators to perform different operations. using System ; class Program { static void Main ( ) { //use of assignment operator to assign a value to a variable int i = 10 ; Console . WriteLine ( i ) ; //Use of arithmatic operators int a = 10 ; int b = 3 ; int sum = a + b ; int subtraction = a - b ; int multiply = a * b ; int divide = a / b ; int reminder = a % b ; Console . WriteLine ( " Addtion: " + sum + " \nSubtraction: " + subtraction + " \nMultiplication: " + multiply + " \

Built-in types in C#

Image
Built-in types in C# Boolean type: only true or false Integral types: sbyte, byte, short, ushort, int, uint, long, ulong, char Floating types: float and double Decimal types String type using System ; class Program { static void Main ( ) { //boolean data type declaration bool b = true ; Console . WriteLine ( b ) ; //Integral types Console . WriteLine ( " sbyte: min value: {0} , max value: {1} " , sbyte . MinValue , sbyte . MaxValue ) ; Console . WriteLine ( " byte: min value: {0} , max value: {1} " , byte . MinValue , byte . MaxValue ) ; Console . WriteLine ( " short: min value: {0} , max value: {1} " , short . MinValue , short . MaxValue ) ; Console . WriteLine ( " ushort: min value: {0} , max value: {1} " , ushort . MinValue , ushort . MaxValue ) ; Console . WriteLine ( " int: min value: {0} , max value: {1} " ,

Reading & Writing to Console

Image
2. Write a C# program to read and write "Hello <first name><last name>" in the console. using System ; class Program { static void Main ( ) { Console . WriteLine ( " Please enter your first name " ) ; string firstName = Console . ReadLine ( ) ; Console . WriteLine ( " Please enter your last name " ) ; string lastName = Console . ReadLine ( ) ; //using concatenation Console . WriteLine ( " Hello " + firstName + " " + lastName ) ; //using placeholder Console . WriteLine ( " Hello {0} {1} " , firstName , lastName ) ; } } Output:

Introduction to C#

1. Write a C# program to print "Hello World" on the console. using System ; class Program { static void Main ( ) { Console . WriteLine ( " Hello world " ) ; } } The namespace declaration, using System, indicates that you are using the System namespace. A namespace is used to organize your code and is a collection of classes. interfaces, structs, enums, and delegates. the main method is the entry point into your application.