Upload
others
View
2
Download
0
Embed Size (px)
Citation preview
Submitted by:-
ZULQARNAIN ASLAM
(ID NO# 14184)
Course: MCS
SEMESTER 4TH
SUBJECT: NETWORK PROGRAMMING
Q1. a) What is C# also discuss Important Feature of C#
b) Write a C# program that takes a number as input and then displays a
rectangle of 3 columns wide and 5 rows tall using that digit.
ANS: a) What is C# also discuss Important Feature of C#
1. C# is a simple, modern, object oriented language derived from C++ and Java.
2. It aims to combine the high productivity of Visual Basic and the raw power of C++.
3. It is a part of Microsoft Visual Studio 7.0.
4. Visual Studio supports VB, VC++, C++, VBscript, JScript. All of these languages provide access to the Microsft .NET platform.
5. .NET includes a Common Execution engine and a rich class library.
6. Microsofts JVM eqiv is Common language run time(CLR).
7. CLR accommadates more than one languages such as C#, VB.NET, Jscript, ASP.NET,C ++.
8. Source code --->Intermediate Language code(IL) ---> (JIT Compiler) Native code.
9. The classes and data types are common to all of the .NET languages.
10. We may develop Console application, Windows application, and Web application using C#.
11. In C#, Microsoft has taken care of C++ problems, such as Memory management, pointers etc.
12. It supports garbage collection, automatic memory management, and a lot.
MAIN FEATURES OF C#
1. SIMPLE
1. Pointers are missing in C#. 2. Unsafe operations such as direct memory manipulation are not
allowed. 3. In C# there is no usage of "::" or "->" operators. 4. Since it's on .NET, it inherits the features of automatic memory
management and garbage collection. 5. Varying ranges of the primitive types like Integer,Floats etc. 6. Integer values of 0 and 1 are no longer accepted as boolean
values.Boolean values are pure true or false values in C# so no more errors of "="operator and "=="operator.
7. "==" is used for comparison operation and "=" is used for assignment operation.
2. MODERN
1. C# has been based according to the current trend and is very powerful and simple for building interoperable, scable, robust applications.
2. C# includes built in support to turn any component into a web service that can be invoked over the internet from any application running on any platform.
3. OBJECT ORIENTED
1. C# supports Data Encapsulation, inheritance,polymorphism, interfaces.
2. (int,float, double) are not objects in java but C# has introduces structures(structs) which enable the primitive types to become objects.
int i=1; string a=i Tostring(); //conversion (or) Boxing
4. TYPE SAFE
1. In C# we cannot perform unsafe casts like convert double to a boolean.
2. Value types (priitive types) are initialized to zeros and reference types (objects and classes) are initialized to null by the compiler automatically.
3. arrays are zero base indexed and are bound checked. 4. Overflow of types can be checked.
5. INTEROPERABILITY
1. C# includes native support for the COM and windows based applications.
2. Allowing restriced use of native pointers. 3. Users no longer have to explicityly implement the unknown and other
COM interfacers, those features are built in. 4. C# allows the users to use pointers as unsafe code blocks to
manipulate your old code. 5. Components from VB NET and other managed code languages and
directlyt be used in C#.
6. SCALABLE AND UPDATEABLE
1. .NET has introduced assemblies which are self describing by means of their manifest. manifest establishes the assembly identity, version, culture and digital signature etc. Assemblies need not to be register anywhere.
2. To scale our application we delete the old files and updating them with new ones. No registering of dynamic linking library.
3. Updating software components is an error prone task. Revisions made to the code. can effect the existing program C# support versioning in the language. Native support for interfaces and method overriding enable complex frame works to be developed and evolved over time.
CONCLUSION
C# is a modern, type safe programming language, object oriented language that enables programmers to quickly and easily build solutions for the Microsoft .NET platform.
MAIN FEATURES OF C#
1. SIMPLE
1. Pointers are missing in C#. 2. Unsafe operations such as direct memory manipulation are not allowed. 3. In C# there is no usage of "::" or "->" operators. 4. Since it's on .NET, it inherits the features of automatic memory management and
garbage collection. 5. Varying ranges of the primitive types like Integer,Floats etc.
6. Integer values of 0 and 1 are no longer accepted as boolean values.Boolean values are pure true or false values in C# so no more errors of "="operator and "=="operator.
7. "==" is used for comparison operation and "=" is used for assignment operation.
2. MODERN
1. C# has been based according to the current trend and is very powerful and simple for building interoperable, scable, robust applications.
2. C# includes built in support to turn any component into a web service that can be invoked over the internet from any application running on any platform.
3. OBJECT ORIENTED
1. C# supports Data Encapsulation, inheritance,polymorphism, interfaces. 2. (int,float, double) are not objects in java but C# has introduces structures(structs)
which enable the primitive types to become objects.
int i=1; string a=i Tostring(); //conversion (or) Boxing
4. TYPE SAFE
1. In C# we cannot perform unsafe casts like convert double to a boolean. 2. Value types (priitive types) are initialized to zeros and reference types (objects
and classes) are initialized to null by the compiler automatically. 3. arrays are zero base indexed and are bound checked. 4. Overflow of types can be checked.
5. INTEROPERABILITY
1. C# includes native support for the COM and windows based applications. 2. Allowing restriced use of native pointers. 3. Users no longer have to explicityly implement the unknown and other COM
interfacers, those features are built in. 4. C# allows the users to use pointers as unsafe code blocks to manipulate your old
code. 5. Components from VB NET and other managed code languages and directlyt be
used in C#.
6. SCALABLE AND UPDATEABLE
1. .NET has introduced assemblies which are self describing by means of their manifest. manifest establishes the assembly identity, version, culture and digital signature etc. Assemblies need not to be register anywhere.
2. To scale our application we delete the old files and updating them with new ones. No registering of dynamic linking library.
3. Updating software components is an error prone task. Revisions made to the code. can effect the existing program C# support versioning in the language. Native support for interfaces and method overriding enable complex frame works to be developed and evolved over time.
CONCLUSION
C# is a modern, type safe programming language, object oriented language that enables programmers to quickly and easily build solutions for the Microsoft .NET platform.
b) Write a C# program that takes a number as input and then displays a
rectangle of 3 columns wide and 5 rows tall using that digit.
C# Sharp Basic: Exercise-13 with Solution
Write a C# program that takes a number as input and then displays a rectangle
of 3 columns wide and 5 rows tall using that digit.
Sample Solution:
C# Sharp Code:
using System;
public class Exercise13
{
public static void Main()
{
int x;
Console.Write("Enter a number: "); x=Convert.ToInt32(Console.ReadLine()); Console.WriteLine("{0}{0}{0}",x); Console.WriteLine("{0} {0}",x); Console.WriteLine("{0} {0}",x); Console.WriteLine("{0} {0}",x); Console.WriteLine("{0}{0}{0}",x); }
}
Q2. Discuss Data types in C# with Programming Examples
Ans:
In C# programming language, Data Types are used to define a type of data the variable can hold such as integer, float, string, etc. in our application.
C# is a Strongly Typed programming language so before we perform any operation on variables, it’s mandatory to define a variable with the required data type to indicate what type of data that variable can hold in our application.
Syntax of Defining C# Data Types
Following is the syntax of defining data types in c# programming language. [Data Type] [Variable Name]; [Data Type] [Variable Name] = [Value]; If you observe the above syntax, we added a required data type before the variable name to tell the compiler about what type of data the variable can hold or which data type the variable belongs to. [Data Type] - It’s a type of data the variable can hold such as integer, string, decimal, etc. [Variable Name] - It’s a name of the variable to hold the values in our application. [Value] - Assigning a required value to the variable. Now we will see how to use the Data Types in our c# applications with examples.
C# Data Types Example
Following is the example of using the data types in c# programming language. namespace Tutlane { class Program { static void Main(string[] args) { int number = 10; string name = "Suresh Dasari"; double percentage = 10.23; char gender = 'M'; bool isVerified = true; } }
} If you observe above c# data types example, we defined a multiple variables with different data types based on our requirements.
Different Data Types in C#
In C# programming language, we have a 3 different type of data types, those are
Type Data Types
Value Data Type int, bool, char, double, float, etc.
Reference Data Type string, class, object, interface, delegate, etc.
Pointer Data Type Pointers.
The following diagram will illustrate more detail about different data types in c# programming language.
C# Value Data Types
In c#, the Value Data Types will directly store the variable value in memory. In c#, the value data types will accept both signed and unsigned literals. The following table lists the value data types in c# programming language with memory size and range of values.
Data Type
.NET Type Size Range
byte Byte 8 bits 0 to 255
Data Type
.NET Type Size Range
sbyte SByte 8 bits -128 to 127
int Int32 32 bits
-2,147,483,648 to 2,147,483,647
uint UInt32 32 bits
0 to 4294967295
short Int16 16 bits
-32,768 to 32,767
ushort UInt16 16 bits
0 to 65,535
long Int64 64 bits
-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
ulong UInt64 64 bits
0 to 18,446,744,073,709,551,615
float Single 32 bits
-3.402823e38 to 3.402823e38
double Double 64 bits
-1.79769313486232e308 to 1.79769313486232e308
Data Type
.NET Type Size Range
bool Boolean 8 bits True or False
decimal Decimal 128 bits
(+ or -)1.0 x 10e-28 to 7.9 x 10e28
DateTime DateTime - 0:00:00am 1/1/01 to 11:59:59pm 12/31/9999
C# Reference Data Types
In c#, the Reference Data Types will contain a memory address of variable value because the reference types won’t store the variable value directly in memory. The following table lists the reference data types in c# programming language with memory size and range of values.
Data Type .NET Type Size Range
string String Variable Length 0 to 2 billion Unicode characters
object Object - -
C# Pointer Data Types
In c#, the Pointer Data Types will contain a memory address of the variable value. To get the pointer details we have two symbols ampersand (&) and asterisk (*) in c# language.
Following is the syntax of declaring the pointer type in c# programming language. type* test; Following is the example of defining the pointer type in c# programming language. int* a; int* b; The following table lists the detail about different type pointer symbols available in c# programming language.
Symbol Name Description
& Address Operator It's useful to determine the address of a variable.
* Indirection Operator
It's useful to access the value of an address.
Q3. Define If-else nested if and switch statement in C# with programming
Examples
Ans:
In this article, we will learn how to use if, if...else, if...else if statement in C# to
control the flow of our program’s execution.
Testing a condition is inevitable in programming. We will often face situations
where we need to test conditions (whether it is true or false) to control the
flow of program. These conditions may be affected by user's input, time factor,
current environment where the program is running, etc.
In this article, we'll learn to test conditions using if statement in C#.
C# if (if-then) Statement
C# if-then statement will execute a block of code if the given condition is true.
The syntax of if-then statement in C# is:
if (boolean-expression)
{
// statements executed if boolean-expression is true
}
The boolean-expression will return either true or false.
If the boolean-expression returns true, the statements inside the body of if (
inside {...} ) will be executed.
If the boolean-expression returns false, the statements inside the body of if
will be ignored.
For example,
if (number < 5)
{
number += 5;
}
In this example, the statement
number += 5;
will be executed only if the value of number is less than 5.
Remember the += operator?
The value of number is initialized to 2. So the expression number < 5 is evaluated
to true. Hence, the code inside the if block are executed. The code after the if
statement will always be executed irrespective to the expression.
Now, change the value of number to something greater than 5, say 10. When
we run the program the output will be:
This statement is always executed.
The expression number < 5 will return false, hence the code inside if block
won't be executed.
C# if...else (if-then-else) Statement
The if statement in C# may have an optional else statement. The block of
code inside the else statement will be executed if the expression is evaluated
to false.
The syntax of if...else statement in C# is:
if (boolean-expression)
{
// statements executed if boolean-expression is true
}
else
{
// statements executed if boolean-expression is false
}
For example,
if (number < 5)
{
number += 5;
}
else
{
number -= 5;
}
In this example, the statement
number += 5;
will be executed only if the value of number is less than 5.
The statement
number -= 5;
will be executed if the value of number is greater than or equal to 5.
***********************************************************************