Thursday, June 20, 2013

Command Line Arguments

Command line arguments are parameters supplied to the Main method at the time of invoking it for execution. To understand the concepts see the example given below:

Program of Command line argument to accept name from the command line and writes to the console.

using System;
class sample
{
       public static void Main( string[ ] args)
  {
       console.write("welcome to");
       console.write(" " +args[0]);
       console.write(" " +args[l]);
  }
}

Execution for this program
C:\ > Sample My Home
Output of Example
Welcome to My Home

In Previous examples, we have used the Main method with no parameters. Notice that in the above example how the Main is declared.
Main is declared with a parameters args. The parameter args is an array of strings. Any arguments provided in the command line at the time of execution, are passed to the array args as its elements. We can access the array elements by using a subscript like args[0], args[1] and so on.

Wednesday, June 19, 2013

Identifiers and Variables

Identifiers refer to the names of variables, functions arrays, classes, etc. created by programmer. They are fundamental requirement of any language. Each language has its own rules for naming these identifiers.
To name the variables of your program, you must follow strict rules. In fact, everything else in your program must have a name.
There are some rules you must follow when naming your objects. On this site, here are the rules we will follow:

• The name must start with a letter or an underscore or the at symbol @.

• After the first letter or underscore, the name can have letters, digits, and/or underscores.

• The name must not have any special characters other than the underscore.

• The name cannot have a space.

• It should not a keyword.

C# is case-sensitive. This means that the names Case, case, and CASE are completely different. For example, the main function is always written Main.

Sunday, June 16, 2013

Boxing and Unboxing in C#

Boxing

Boxing is an implicit conversion of a value type to the type object or to any interface type implemented by this value type. Boxing a value of a value allocated an object instance and copies the values into the new object.
Consider the following declaration of a value-type variable:

int i = 123;
The following statement implicitly applies the boxing operation on the variables:
Object o = i;

The result of this statement is creating an object, on the stack, that references a value of the type int, on the heap. This value is a copy of the value-type value assigned to the variable i. The difference between the two variables, i and 0 is illustrated in the following figure.
Boxing is an implicit conversion of a value type to the type object or to any interface type implemented by this value type.
It also possible, but never needed to perform the boxing explicitly as in the following example

Int i = 123;
Object O = (object) i;

Unboxing

Unboxing is an explicit conversion from the object to value type or from an interface type to a value type that implements the interface. An unboxing operation consists of:
• Checking the object instance to make sure it is a boxed value of the given value type.
• Copying the value from the instance into the value-type variable.

The following statements demonstrate both boxing and unboxing operations:
Int i = 123;       // a value type
Object O = i;   //boxing
int j = (int) O;   // unboxing

Unboxing is an explicit conversion from the object to value type or from an interface type to a value type that implements the interface.

For the unboxing of value types to succeed at runtime, the item being unboxed must be a reference to an object that was previously created by boxing an instance of that value type. Attempting to unbox null or a reference to an incompatible value type will return an invaIidcastException.

Saturday, June 15, 2013

C# Data Types

C# is a type-safe language Variables are declared as being of a particular type and each variable is constrained to hold only values of its declared type.
Variables can hold either value types or reference types, or they can be pointers. Here's a quick recap of the difference between value types and reference types.

• Where a variable v contains a value type, it directly contains an object with some value. No other variable v' can directly contain the object contained by v (although v' might contain an object with the same value).

• Where a variable v contains a reference type, what it directly contains is something which refers to an object. Another variable v' can contain a reference to the same object referred to by v.

Value Types

C# defines the following value types:

    Primitives int i;
    Enum enum state {off, on }
    Struct struct Point{int x, y; }

It is possible in C# to define your own value types by declaring enumerations or structs. These user-defined types are mostly treated in exactly the same way as C#'s predefined value types, although compilers are optimized for the latter. In C# all of the apparently fundamental value types are in fact built up from the (actually fundamental) object type.

Reference Types

The pre-defined reference types are object and string, where object - is the ultimate base class of all other types. New reference types can be defined using 'class', 'interface', and 'delegate' declarations. Therefore the reference types are:

  1. Predefined Reference Types
• Object                          • String

  1. User Defined Reference Types
• Classes                      • Interfaces
• Delegates                  • Arrays

Reference types actually hold the value of a memory address occupied by the object they reference.

Friday, June 14, 2013

Features of C#

Simplicity

All the Syntax of java is like C++. There is no preprocessor and much larger library. C# code does not require header files. All code is written inline.

Consistent Behavior

C# introduced a unified type system which eliminates the problem of varying ranges of integer types. All types are treated as objects and developers can extend the type system simply and easily.

Modern Programming Language

C# supports number of modem features, such as:
• Automatic Garbage Collection
• Error handling features
• Modern debugging features
• Robust Security features

Pure Object-Oriented Programming Language

In C#, everything is an object. There are no more global functions, variable and constants. It supports all three object oriented features:
• Encapsulation
• Inheritance
• Polymorphism

Type Safety

Type safety promotes robust programming. Some examples of type safety are:
• All objects and arrays are initialized by zero dynamically
• An error message will be produced, on use of any uninitialized variable
• Automatic checking of array (out of bound and etc.)

Feature of Versioning

Making new versions of software module work with the existing applications is known as versioning. It is achieved by the keywords new and override.

Compatible with other Language

C# enforces the .NET common language specifications (CLS) and therefore allows interoperation with other .NET language.

Inter-operability

Language interoperability is the ability of code to interact with code that is written using a different programming language. Language interoperability can help maximize code reuse and, therefore, improve the efficiency of the development process. C# provides support for using COM objects, no matter what language was used to author them. C# also supports a special feature that enables a program to call out any native API.

Wednesday, June 12, 2013

Introduction to C#

Microsoft Corporation developed a new computer programming language C# pronounced as 'C- Sharp'. C# is a simple, modem, object oriented, and type safe programming language derived from C and C++. C# is a purely object-oriented language like as Java. It has been designed to support the key features of .NET framework. Like Java, C# is a descendant language of C++ which is descendant of C language.
C# modernizes C++ by enhancing some of its features and adding a few new features. C# borrows Java's features such as grouping of classes, interface and implementation together in one file so the programmers can easily edit the codes. C# also handles objects using reference, the same way as Java.

Comparing C# to C++ and Java

C# and Java are both new-generation languages descended from a line including C and C++. Each includes advanced features, like garbage collection, which remove some of the low level maintenance tasks from the programmer. In a lot of areas they are syntactically similar.
Both C# and Java compile initially to an intermediate language: C# to Microsoft Intermediate Language (MSIL), and Java to Java byte code. In each case the intermediate language can be run - by interpretation or just-in-time compilation on an appropriate 'virtual machine'. In C#, however, more support is given for the further compilation of the intermediate language code into native code. C# contains more primitive data types than Java, and also allows more extension to the value types. For example, C# supports 'enumerations', type-safe value types which are limited to a defined set of constant variables, and 'structs', which are user-defined value types.

Unlike Java, C# has the useful feature that we can overload various operators.
Like Java, C# gives up on multiple class inheritance in favor of a single inheritance model extended by the multiple inheritances of interfaces. However, polymorphism is handled in a more complicated fashion; with derived class methods either 'overriding' or 'hiding' super class methods
C# also uses 'delegates'-type-safe method pointers. These are used to implement event handling.

C# versus C++

Although it has some elements derived from Visual Basic and Java, C++ is C#'s closest relative. In an important change from C++, C# code does not require header files. All code is written inline.
As touched on above, the .NET runtime in which C# runs performs memory management, taking care of tasks like garbage collection. Because of this, the use of pointers in C# is much less important than in C++. Pointers can be used in C#, where the code is marked as 'unsafe', but they are only really useful in situations where performance gains are at an absolute premium.
Speaking generally, the 'plumbing' of C# types is different from that of C++ types, with all C# types being ultimately derived from the 'object' type. For instance, C# arrays are bounds checked unlike in C++, and it is therefore not possible to write past the end of a C# array.
C# statements are quite similar to C++ statements. To note just one example of a difference: the 'switch' statement has been changed so that 'fall-through' behavior is disallowed.

        As mentioned above, C# gives up on the idea of multiple class inheritance. Other differences relating to the use of classes are: there is support for class 'properties' of the kind found in Visual Basic, and class methods are called using the Operator rather than the :: operator.

Monday, June 10, 2013

Reflection

.NET Framework's Reflection API allows you to fetch type (assembly) information at runtime programmatically. We can also achieve late binding by using .NET Reflection. At runtime, the Reflection mechanism uses the PE file to read information about the assembly. Reflection enables you to use code that is not available at compile time. .NET Reflection allows an application to collect information about itself and also to manipulate on itself. It can be used effectively to find all types in an assembly and/or dynamically invoke methods in an assembly. This includes information about the type, properties, methods, and events of an object. With Reflection, we can dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object and invoke its methods or access its fields and properties. We can also access attribute information using Reflection.
Using Reflection, you can get any kind of information which you can see in a class viewer; for example, information on the methods, properties, fields, and events of an object.
The System.Reflection namespace and the System.Type class plays a very important role in .NET Reflection. These two work together and allow you to reflect over many other aspects of a type.