By the help of C# programming language, we can develop different types
of secured and robust applications:
o Window applications
o Web applications
o Distributed applications
o Web service applications
o Database applications etc.
Java vs C#
There are many differences and similarities between Java and C#. A list of
top differences between Java and C# are given below:
No Java C#
.
1) Java is a high level, robust, secured C# is an object-oriented
and object-oriented programming language developed
programming language developed by by Microsoft that runs on .Net
Oracle. Framework.
2) Java programming language is designed C# programming language is
to be run on a Java platform, by the help designed to be run on the Common
of Java Runtime Environment (JRE). Language Runtime (CLR).
3) Java type safety is safe. C# type safety is unsafe.
4) In java, built-in data types that are In C#, built-in data types that are
passed by value are called primitive passed by value are called simple
types. types.
5) Arrays in Java are direct specialization Arrays in C# are specialization
of Object. of System.
6) Java does not support conditional C# supports conditional compilation
compilation. using preprocessor directives.
7) Java doesn't support goto statement. C# supports goto statement.
8) Java doesn't support structures and C# supports structures and unions.
unions.
9) Java supports checked exception and C# supports unchecked exception.
unchecked exception.
Anders Hejlsberg is known as the founder of C# language.
C# Data Types
A data type specifies the type of data that a variable can store such as
integer, floating, character etc.
There are 3 types of data types in C# language.
Types Data Types
Value Data Type short, int, char, float, double etc
Reference Data Type String, Class, Object and Interface
Pointer Data Type Pointers
Value Data Type
The value data types are integer-based and floating-point based. C#
language supports both signed and unsigned literals.
There are 2 types of value data type in C# language.
1) Predefined Data Types - such as Integer, Boolean, Float, etc.
2) User defined Data Types - such as Structure, Enumerations, etc.
The memory size of data types may change according to 32 or 64 bit
operating system.
Data Types Memory Size Range
char 1 byte -128 to 127
signed char 1 byte -128 to 127
unsigned char 1 byte 0 to 127
short 2 byte -32,768 to 32,767
signed short 2 byte -32,768 to 32,767
unsigned 2 byte 0 to 65,535
short
int 4 byte -2,147,483,648 to -2,147,483,647
signed int 4 byte -2,147,483,648 to -2,147,483,647
unsigned int 4 byte 0 to 4,294,967,295
long 8 byte ?9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
signed long 8 byte ?9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
unsigned long 8 byte 0 - 18,446,744,073,709,551,615
float 4 byte 1.5 * 10-45 - 3.4 * 1038, 7-digit precision
double 8 byte 5.0 * 10-324 - 1.7 * 10308, 15-digit precision
decimal 16 byte at least -7.9 * 10?28 - 7.9 * 1028, with at least 28-digit
precision
Let's see the value data types. It size is given according to 32 bit OS.
Reference Data Type
The reference data types do not contain the actual data stored in a
variable, but they contain a reference to the variables.
If the data is changed by one of the variables, the other variable
automatically reflects this change in value.
There are 2 types of reference data type in C# language.
1) Predefined Types - such as Objects, String.
2) User defined Types - such as Classes, Interface.
Pointer Data Type
The pointer in C# language is a variable, it is also known as locator or
indicator that points to an address of a value.
AD
Symbols used in pointer
Symbol Name Description
& (ampersand sign) Address operator Determine the address of a variable.
* (asterisk sign) Indirection operator Access the value of an address.
Declaring a pointer
The pointer in C# language can be declared using * (asterisk symbol).
1. int * a; //pointer to int
2. char * c; //pointer to char
In this example, we are getting input from the user
using Console.ReadLine() method. It returns string. For numeric value,
you need to convert it into int using Convert.ToInt32() method.
1. using System;
2. public class IfExample
3. {
4. public static void Main(string[] args)
5. {
6. Console.WriteLine("Enter a number:");
7. int num = Convert.ToInt32(Console.ReadLine());
8.
9. if (num % 2 == 0)
10. {
11. Console.WriteLine("It is even number");
12. }
13. else
14. {
15. Console.WriteLine("It is odd number");
16. }
17.
18. }
19. }
Output:
Enter a number:11
It is odd number
Output:
Enter a number:12
It is even number
C# If else-if Example
1. using System;
2. public class IfExample
3. {
4. public static void Main(string[] args)
5. {
6. Console.WriteLine("Enter a number to check grade:");
7. int num = Convert.ToInt32(Console.ReadLine());
8.
9. if (num <0 || num >100)
10. {
11. Console.WriteLine("wrong number");
12. }
13. else if(num >= 0 && num < 50){
14. Console.WriteLine("Fail");
15. }
16. else if (num >= 50 && num < 60)
17. {
18. Console.WriteLine("D Grade");
19. }
20. else if (num >= 60 && num < 70)
21. {
22. Console.WriteLine("C Grade");
23. }
24. else if (num >= 70 && num < 80)
25. {
26. Console.WriteLine("B Grade");
27. }
28. else if (num >= 80 && num < 90)
29. {
30. Console.WriteLine("A Grade");
31. }
32. else if (num >= 90 && num <= 100)
33. {
34. Console.WriteLine("A+ Grade");
35. }
36. }
37. }
Output:
Enter a number to check grade:66
C Grade
Output:
Enter a number to check grade:-2
wrong number