Beruflich Dokumente
Kultur Dokumente
I have found some interesting advantages of C# over Java and for that I have also
won the prize from the Java team...!!. So I want to share it with all people so
others can also know about it.
C# being a .NET language, it supports language interoperability, i.e. C# can access
code written in any .NET compliant language and can also inherit the classes
written in these languages. This is not possible in Java.
The code written in C#, on compilation generates an ‘.exe' or ‘.dll' file which is
also called Portable Executable file. These files contain MSIL (Microsoft
Intermediate Language) code. As against this, the Java code on compilation
generates a ‘.class' file, which contains bytecode.
The portable executable file of C# can contain any number of classes, whereas, the
‘.class' file in Java contains only one class.
The methods in C# are not virtual by default. On the contrary, methods in Java are
virtual by default, which degrades the performance.
The classes in C# are grouped in Namespaces, whereas, classes in Java are grouped
in Packages.
The C# namespaces are not related to the directories. The packages in Java are
directly related with the directory names.
The variables of primitive data types in C# are more powerful. This is because even
though they are not objects functions can be called using them. The variables of
primitive data types in Java cannot call functions.
C# has features like Properties and Indexers. These features are not available in
the Java language.
Through C# we can easily call Windows API function and access COM components which
is quite difficult in Java.
For more follow the link:
Advantages of C# Over Java
2. Future of C#
Today, C# is not only a Windows development programming language but can be used to
build Web applications, Windows store apps, and mobile apps including iOS and
Android. C# can also do more than that. If you’ve not already read my article, I
highly recommend going and reading What C# Can Do For You.
At the Build 2016 event, Microsoft made several exciting announcements and one of
them was integrating Xamarin as a part of Visual Studio “15” and beyond. Now C#
developers can build iOS and Android apps that can spit out native iOS and Android
code.
In the following Channel 9 video, Microsoft’s Dustin Campbell and Mads Torgersen
talk about the future of C#.
C# can be used to build Windows client apps, Windows Store apps, iOS apps, and
Android aps and can also be used to build backend and middle-tier frameworks and
libraries.
C# 7 comes with new features including tuples, record types, and pattern matching.
Unlike other programming languages, C# is still young and evolving. Now being open
sourced, C# is getting community involvement and new features are being decided by
community. The following table summarizes the improvements made in each newer
version of the language.
Version
Year
Key features introduced
1.0
Jan 2002
1.2
Oct 2003
Modern, object-oriented, type safe, automatic memory management, versioning control
2.0
Sept 2005
Generics, partial classes, anonymous types, iterators, nullable types, static
classes, delegate interface
3.0
Aug 2007
Implicit types, object and collection initializers, auto-implemented properties,
extension methods, query and lambda expressions, expression trees, partial methods.
4.0
April 2010
Dynamic binding, named and optional arguments, Generic covariance and
Contravariance, Embedded interop types.
5.0
June 2013
Async methods, Caller info Attributes
6.0
July 2015
Roslyn (compiler-as-a-service), exception filters, Await in catch/finally block,
auto property initializer, string interpolation, nameof operator, dictionary
initializer
7.0
2016
Tuples, pattern matching, record types, local functions, Async streams
For more follow the link:
The word abstract means a concept or an idea not associated with any specific
instance. In programming we apply the same meaning of abstraction by making classes
not associated with any specific instance. The abstraction is done when we need to
only inherit from a certain class, but do not need to instantiate objects of that
class. In such case the base class can be regarded as "Incomplete". Such classes
are known as an "Abstract Base Class".
Class having abstract keyword and having abstract keyword with some of its methods
(not all) is known as an Abstract Base Class.
Class having Abstract keyword and having abstract keyword with all of its methods
is known as pure Abstract Base Class.
An abstract class holds the methods but the actual implementation of those methods
is made in derived class.
Lets have a look at this code!
In the derived class we have the same named method but this method has its body.
We are doing abstraction here so that we can access the method of derived class
without any trouble.
class program
{
abstract class animal
{
public abstract void eat();
public void sound()
{
Console.WriteLine("dog can sound");
}
}
class dog: animal
{
public override void eat()
{
Console.WriteLine("dog can eat");
}
}
static void Main(string[] args)
{
dog mydog = new dog();
animal thePet = mydog;
thePet.eat();
mydog.sound();
}
}
Finally we created an Object 'mydog' of class dog, but we didn't instantiate any
object of Abstract Base Class 'animal'.
This is an object which is declared as thePet and its data type is the abstract
base class 'animal'.
In the above code we declare an Object 'thePet', of the type animal (the Abstract
Base Class) and simply copy the object of another object (only the reference is
copied as they belong to reference type). Now we can use object 'thePet' just as
object 'mydog'.
The object oriented programming will give the impression very unnatural to a
programmer with a lot of procedural programming experience. In Object Oriented
programming Encapsulation is the first place. Encapsulation is the procedure of
covering up of data and functions into a single unit (called class). An
encapsulated object is often called an abstract data type. In this article let us
see about it in a detailed manner.
The need of encapsulation is to protect or prevent the code (data) from accidental
corruption due to the silly little errors that we are all prone to make. In Object
oriented programming data is treated as a critical element in the program
development and data is packed closely to the functions that operate on it and
protects it from accidental modification from outside functions.
Let us see an example of Department class. To manipulate the data in that class
(String departname) we define an accessor (get method) and mutator (set method).
using system;
public class Department
{
private string departname;
.......
// Accessor.
public string GetDepartname()
{
return departname;
}
// Mutator.
public void SetDepartname(string a)
{
departname = a;
}
}
Like the above way we can protect the private data from the outside world. Here we
use two separate methods to assign and get the required data.
In the language of C#, a class that is inherited is called a base class. The class
that does the inheriting is called the derived class. Therefore a derived class is
a specialized version of a base class. It inherits all of the variables, methods,
properties, and indexers defined by the base class and adds its own unique
elements.
Example
Diagram
The following diagram shows the inheritance of a shape class. Here the base class
is Shape and the other classes are its child classes. In the following program we
will explain the inheritance of the Triangle class from the Shape class.
Info for t1
Triangle is isosceles
Width and height are 4 and 4
Area is 8
Info for t2:
Triangle is right
Width and height are 8 and 12
Area is 48
Polymorphism means the same operation may behave differently on different classes.
class A1
{
void hello()
{
Console.WriteLine("Hello");
}
void hello(string s)
{
Console.WriteLine("Hello {0}", s);
}
}
Example of Run Time Polymorphism
Method Overriding: Method overriding occurs when child class declares a method that
has the same type arguments as a method declared by one of its superclass.
Note
By default functions are not virtual in C# and so you need to write “virtual”
explicitly. While by default in Java each function are virtual.
Class parent
{
virtual void hello()
{
Console.WriteLine("A D Patel");
}
}
Class child: parent
{
override void hello()
{
Console.WriteLine("R A Patel");
}
}
static void main()
{
parent objParent = new child();
objParent.hello();
}
//Output
R A Patel
For more follow the link:
Polymorphism in C#
7. Delegates in C#
Delegate is one of the base types in .NET. Delegate is a class, which is used to
create delegate at runtime.
Need of delegate
There might be a situation in which you want to pass methods around to other
methods. For this purpose we create delegate.
Example
This ability to refer to a method as a parameter makes delegates ideal for defining
callback methods.
Delegate magic
In class we create its object, which is instance, but in delegate when we create
instance that is also referred to as delegate (means whatever you do you will get
delegate).
Delegate does not know or care about the class of the object that it references.
Any object will do; all that matters is that the method's argument types and return
type match the delegate's. This makes delegates perfectly suited for "anonymous"
invocation.
Benefit of delegates
In simple words delegates are object oriented and type-safe and very secure as they
ensure that the signature of the method being called is correct. Delegate helps in
code optimization.
Types of delegates
Singlecast delegates
Multiplecast delegates
Delegate is a class. Any delegate is inherited from base delegate class of .NET
class library when it is declared. This can be from either of the two classes from
System.Delegate or System.MulticastDelegate.
Singlecast delegate
Multicast Delegate
When a delegate is wrapped with more than one method that is known as a multicast
delegate.
In C#, delegates are multicast, which means that they can point to more than one
function at a time. They are derived from System.MulticastDelegate class.
ArrayList Collection
An ArrayList is a dynamic array and implements the IList interface. Each element is
accessible using the indexing operator. While the traditional array has a fixed
number of elements, in the case of Array List, elements can be added or removed at
run time.
Here you can use the new keyword to create an instance of the ArrayList object. You
don't need to specify the size. Once you have an empty ArrayList object, you can
use the Add() method to add elements to it,as in,
obj.Add("item1");
obj.Add("2");
obj.Add("Delhi");
Each new item in the ArrayList is added to the end of the list, so it has the
largest index number. If you wanted to add an item in the middle of the list, then
you can use "Insert()" with a numeric argument as follows,
Obj.Insert(2,"item2");
You can also remove members of the ArrayList using either Remove() or RemoveAt()
methods as in the following,
Obj.Remove("item1");
Obj.RemoveAt(3);
Benefits of ArrayLists
Insert Elements: An ArrayList starts with a collection containing no elements. You
can add them in any position as you choose them.
Automatic Resizing: you do not need to specify the array size; as you add elements,
the array automatically ensures there is enough memory for the array.
Flexibility When Removing Elements: you can remove any element from an Arraylist
very easily.
Limitations of ArrayLists
Note - ArrayLists are slower and more resource-intensive than a conventional array.
The following example shows an array list with an #ff0000 size. Elements are added
dynamically as required. We are adding elements using the "Add()" method as well as
using the "Insert()" method at a specific location. Later we are displaying all the
elements by iterating through the list.
using System;
using System.Collections;
namespace CollectionApp
{
class Program
{
static void Main(string[] args)
{
//Defining an ArrayList
ArrayList obj = new ArrayList();
//Adding elements
obj.Add("India");
obj.Add("USA");
obj.Add("Russia");
obj.Add("300");
//Adding elements to specific position
obj.Insert(2, "Japan");
//Accessing elements
for (int i = 0; i < obj.Count; i++)
{
Console.WriteLine("At Index[" + i + "]= " + obj[i].ToString());
}
Console.WriteLine("____________");
Console.WriteLine("Press any key");
Console.ReadKey();
}
}
}
After building and running this program, the output of the program is as in the
following,
9. Exception handling in C#
C# provides the three keywords try, catch and finally to do exception handling. The
try block encloses the statements that might throw an exception whereas catch
handles an exception if one exists. The finally can be used for doing any clean-up
process.
try
{
// Statement which can cause an exception.
} catch (Type x)
{
// Statements for handling the exception
} finally
{
//Any cleanup code
}
If any exception occurs inside the try block then the control transfers to the
appropriate catch block and later to the finally block.
But in C#, both catch and finally blocks are optional. The try block can exist
either with one or more catch blocks or a finally block or with both catch and
finally blocks.
If there is no exception occurring inside the try block then the control directly
transfers to the finally block. We can say that the statements inside the finally
block is executed always. Note that it is an error to transfer control out of a
finally block by using break, continue, return or goto.
In C#, exceptions are nothing but objects of the type Exception. The Exception is
the ultimate base class for any exceptions in C#. The C# itself provides a couple
of standard exceptions. Or even the user can create their own exception classes,
provided that this should inherit from either the Exception class or one of the
standard derived classes of the Exception class like DivideByZeroExcpetion or
ArgumentException and so on.
The System.IO namespace provides four classes that allow you to manipulate
individual files, as well as interact with a machine directory structure. The
Directory and File directly extends System.Object and supports the creation,
copying, moving and deletion of files using various static methods. They only
contain static methods and are never instantiated. The FileInfo and DirecotryInfo
types are derived from the abstract class FileSystemInfo type and they are
typically, employed for obtaining the full details of a file or directory because
their members tend to return strongly typed objects. They implement roughly the
same public methods as a Directory and a File but they are stateful and the members
of these classes are not static.
In the .NET framework, the System.IO namespace is the region of the base class
libraries devoted to file based input and output services. Like any namespace, the
System.IO namespace defines a set of classes, interfaces, enumerations, structures
and delegates. The following table outlines the core members of this namespace.
Class Types
Description
Directory/ DirectoryInfo
These classes support the manipulation of the system directory structure.
DriveInfo
This class provides detailed information regarding the drives that a given machine
has.
FileStream
This gets you random file access with data represented as a stream of bytes.
File/FileInfo
These sets of classes manipulate a computer's files.
Path
It performs operations on System.String types that contain file or directory path
information in a platform-neutral manner.
BinaryReader/ BinaryWriter
These classes allow you to store and retrieve primitive data types as binary
values.
StreamReader/StreamWriter
Used to store textual information to a file.
StringReader/StringWriter
These classes also work with textual information. However, the underlying storage
is a string buffer rather than a physical file.
BufferedStream
This class provides temp storage for a stream of bytes that you can commit to
storage at a later time.
The System.IO provides a class DriveInfo to manipulate the system drive related
tasks. The DriveInfo class provides numerous details such as the total number of
drives, calculation of total hard disk space, available space, drive name, ready
status, types and so on. Consider the following program that shows the total disk
drives.
DriveInfo[] di = DriveInfo.GetDrives();
Console.WriteLine("Total Partitions");
using System;
using System.IO;
namespace DiskPartition
{
class Program
{
static void Main(string[] args)
{
DriveInfo[] di = DriveInfo.GetDrives();
Console.WriteLine("Total Partitions");
Console.WriteLine("---------------------");
foreach(DriveInfo items in di)
{
Console.WriteLine(items.Name);
}
Console.Write("\nEnter the Partition::");
string ch = Console.ReadLine();
Console.ReadKey();
}
}
}
After compiling this program, it displays nearly every detail of disk drives and a
specific drive as in the following,
Reading and writing operations are done using a File object. The following code
snippet reads a text file located in the machine somewhere.
Besides reading a file, we can write some contents over an existing text file by
the File class WriteAllTest() method as in the following,
File.WriteAllText(@"d:\test.txt", textBox2.Text);
It takes a path to save the file and content input method medium such as a text box
or any other control. The following images depict a text file reading by entering
its corresponding path,
Stream
Console.WriteLine(Encoding.Default.GetString(rFile));
}
BinaryReader and BinaryWriter
The BinaryReader and Writer class allows you to read and write discrete data types
to an underlying stream in a compact binary format. The BinaryWriter class defines
a highly overloaded Write method to place a data type in the underlying stream.
Members
Description
Class
Write
Write the value to current stream
BinaryWriter
Seek
Set the position in the current stream
BinaryWriter
Close
Close the binary reader
BinaryWriter
Flush
Flush the binary stream
BinaryWriter
PeekChar
Return the next available character without advancing the position in the stream
BinaryReader
Read
Read a specified set of bytes or characters and store them in the incoming array.
BinaryReader
For more examples follow the link:
File Handling in C# .NET