Tuesday, 6 August 2013

c# basic structure

let we see the basic example 
 
File Hello.cs
 
 
 
using System;
class Hello {
static void Main()
{
Console.WriteLine("Hello World");
}
}

C# is very similar to java:

(70% of java,10% of c++,5% vb,15% of new)




But compare to java, We can say differentiate some main concepts.Let we discuss the difference between java and our c#.

As in Java
  • Object-orientation (single inheritance)

  • Interfaces

  • Exceptions

  • Threads

  • Namespaces (like Packages)

  • Strong typing

  • Garbage Collection

  • Reflection

  • Dynamic loading of code

Really new
(compared to Java)
* Reference and output parameters

* Objects on the stack (structs) 

* Rectangular arrays

* Enumerations

* Unified type system

* goto 

* Versioning

Monday, 5 August 2013

UnBoxing Conversions


UnBoxing is the explicit conversion from a reference type to a value type or from an
interface type to a value type that implements the interface.
When unboxing occurs, memory is copied from the managed heap to the stack. For an
unboxing conversion to a given value type to succeed at run time, the value of the source
argument must be a reference to an object that was previously created by boxing a value
of that value type otherwise an exception is thrown.
Example:
int n = 10;
int j;
Object obj;
obj = n;
j = (int)obj;
Explanation:
In the above code segment, another integer variable
j
is declared. The last statement
performs explicit conversion of object-type to value-type i.e. integer.
Boxing and UnBoxing have performance implications. Every time a value type is boxed,
a new reference type is created and the value type is copied onto the managed heap.
Depending on the size of the value type and the number of times value types are boxed
and unboxed, the CLR can spend a lot of CPU cycles just doing these conversions.
It is recommended to perform boxing and unboxing in a scenario where you have to pass
a value parameter multiple times to a method that accepts a reference parameter. In such
a case, it is advantageous to box the value parameter once before passing it multiple times
to methods that accept reference methods.

Boxing Conversions

Boxing is the implicit conversion of a value type to a reference type or to any interface
type implemented by this value type. This is possible due to the principle of type system
unification where everything is an object.
When boxing occurs, the contents of value type are copied from the stack into the
memory allocated on the managed heap. The new reference type created contains a copy
of the value type and can be used by other types that expect an object reference. The
value contained in the value type and the created reference types are not associated in any
way. If you change the original value type, the reference type is not affected. Boxing,
thus, enables everything to appear to be an object, thereby avoiding the overhead required
if everything actually were an object. 
Example: 
int n = 10;
Object obj;
obj = n; 
Explanation: 
In the above code segment, a
value-type
variable
n
is declared and is assigned the value
10
. The next statement declares an
object-type
variable
obj
. The last statement implicitly
performs
boxing
operation on the variable
n
.

Why Namespaces

Namespaces are used in .Net to organize class libraries into a hierarchical structure and
reduce conflicts between various identifiers in a program. By helping organize classes,
namespaces help programmers manage their projects efficiently and in a meaningful way
that is understood by consumers of the class library. Namespaces enables reusable
components from different companies to be used in the same program without the worry
of ambiguity caused by multiple instances of the same identifier.
Namespaces provide a logical organization for programs to exist. Starting with a top-
level namespace, sub-namespaces are created to further categorize code, based upon its
purpose.
In .Net, the base class library begins at the
System
namespace. There are several classes
at the
System
level such as
Console
,
Exception
etc. The namespace name gives a good
idea of the types of classes that are contained within the namespace. The fully qualified
name of a class is the class name prefixed with the namespace name. There are also
several nested namespaces within the
System
namespace such as
System.Security
,
System.IO
,
System.Data, System.Collections
etc.
Reducing conflict is the greatest strength of namespaces. Class and method names often
collide when using multiple libraries. This risk increases as programs get larger and
include more third-party tools

Class vs. Structures

Struct – Code:
using System;
class Test {
int classvar ;
int anothervar =20;
public Test ( )
{
classvar = 28;
}
public static void Main()
{
Test t = new Test();
ExampleStruct strct = new ExampleStruct(20);
System.Console.WriteLine(strct.i);
strct.i = 10;
System.Console.WriteLine(t.classvar);
System.Console.WriteLine(strct.i);
strct.trialMethod();
}
struct ExampleStruct {
public int i;
public ExampleStruct(int j)
{
i = j;

}
public void trialMethod()
{
System.Console.WriteLine("Inside Trial Method");
}
}
O/P:-
28
20
10
Inside Trial Method


In the above example, I have declared and used a constructor with a single parameter for
a structure. Instead if I had tried to use a default parameter-less parameter I would have
got an error. But the same is possible in the case of classes as shown by the default
parameter-less constructor, which initializes the classvar variable to 28.
Another point to note is that a variable called anothervar has been declared and initialized
within the class whereas the same cannot be done for members of a structure.

How to make a Property Read Only/Write Only


There are times when we may want a property to be read-only – such that it can’t be
changed.
This is where read-only properties come into the picture. A Read Only property is one
which includes only the get accessor, no set accessor.
public read Only int empid
{
get
{
return empid;
}
}
Similar to read-only properties there are also situations where we would need
something known as write-only properties. In this case the value can be changed
but not retrieved. To create a write-only property, use the WriteOnly keyword and
only implement the set block in the code as shown in the example below.
public writeOnly int e
{
set
{
e = value
}
}

Get accessor

Get accessor
The execution of the
get
accessor is equivalent to reading the value of the field.
The following is a
get
accessor that returns the value of a private field name:
private string name; // the name field
public string Name // the Name property
{
get
{
return name;
}
}
Set accessor
The
set
accessor is similar to a method that returns
void
. It uses an implicit parameter
called
value
, whose type is the type of the property. In the following example, a
set
accessor is added to the Name property:
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
When you assign a value to the property, the
set
accessor is invoked with an argument
that provides the new value. For example:
e1.Name = "Reshmi"; // The set accessor is invoked here
It is an error to use the implicit parameter name (
value
) for a local variable declaration in
a
set
accessor.

Overloading and Overriding of the Class

Overloading and Overriding of the Class
Overloading provides the ability to create multiple methods or properties with the same
name, but with different parameters lists. This is a feature of polymorphism. A simple
example would be an addition function, which will add the numbers if two integer
parameters are passed to it and concatenate the strings if two strings are passed to it. 
 
 
using System;
public class test
{
public int Add(int x , int y)
{
return(x + y);
}
public string Add(String x, String y )
{
return (x + y);
}
public static void Main()
{
test a = new test ();
int b;
String c;
b = a.Add(1, 2);
c = a.Add("Reshmi", " Nair");
Console.WriteLine(b);
Console.WriteLine(c);
}
}
O/P:
3
Reshmi Nair
 
 
 
 
Overriding 
 
Class inheritance causes the methods and properties present in the base class also to be
derived into the derived class. A situation may arise wherein you would like to change
the functionality of an inherited method or property. In such cases we can override the
method or property of the base class. This is another feature of polymorphism.
public abstract class shapes
{
public abstract void display()
{
Console.WriteLine("Shapes");
}
}
public class square: shapes
{
public override void display()
{
Console.WriteLine("This is a square");
}
}
public class rectangle:shapes
{
public override void display()
{
Console.WriteLine("This is a rectangle");
}
}
The above example is just an indication to how overriding can be implemented in C#.
 
 

Class Type


Custom data types are available in .NET framework in the form of classes or class type. It
is nothing but a set of data and related behavior that is defined by the developer.
Object type and class type are both reference type variables. The only difference comes
from the fact that object type consists of objects predefined and available with the .NET
framework such as string whereas class type consists of custom user defined data types
such as the class employee given below.

class employee
{
int empid;
string empname
public employee()
{
empid = 10;
empname = “YOGESWARAN”;
}
}

Object Type


Object type or reference type variables are those, which are allocated storage space in the
heap. Reference type objects can be null. When a reference type is allocated under the
covers a value is allocated on the heap and a reference to that value is returned. There are
basically four reference types: classes, interfaces, delegates and arrays.

Simple Types (Primitive Data types)

Simple or value type variables are those, which are assigned space in the stack instead of
the heap. All the primitive types such as int, double etc are value type variables. The
simple types basically consist of Boolean and Numeric types, where Numeric is further
divided into Integral and Floating Point.
The first rule of value types is that they cannot be null. Anytime you declare a variable of
value type, you have allocated the number of bytes associated with that type on the stack
and are working directly with that allocated array of bits. In addition, when you pass a
variable of value type, you are passing that variable’s value and not a reference to the
underlying object.

INTRODUCTION TO C#

.NET framework offers a myriad of languages which puts us programmers into a deep
thought process about which programming language best suits our needs.
Which language is the "best" language choice? If you are a VB wizard, should you take
the time to learn C# or continue to use VB.NET? Are C# ASP.NET pages "faster" than
VB .NET ASP.NET pages? These are questions that you may find yourself asking,
especially when you're just starting to delve into .NET. Fortunately the answer is simple:
there is no "best" language. All .NET languages use, at their root, functionality from the
set of classes provided by the .NET Framework. Therefore, everything you can do in
VB.NET you can do in C#, and vice-a-versa.

Constants & Variables

A variable is a named memory location. They are programming elements that can change
during program execution. Data that needs to be stored in memory & accessed at a later
time are stored in variables. Instead of referring to the memory location by the actual
memory address you refer to it with a variable name. 
Variables are declared as follows 
int a;
They can also be initialized at the time of declaration as follows:
int a = 10;


Constants are very similar to variables. The main difference is that the value contained in
memory cannot be changed once the constant is declared. When you declare a constant
its value is also specified and this value cannot be changed during program execution.
Constants are used in situations where we need to keep the value in some memory
location constant. If you use hard-coded values, and the value is changed then it has to be
changed in all the locations in the code where it has been used. Instead if we are using
constants, all we will need to do is to change the value of the constant. This would
propagate the changes to our entire application.
Constants are declared as follows
const int a;

.NET DEBUGGING

.NET Debugging 


Debugging is the most important feature of any programming language and Visual Studio
.NET IDE provides this feature in an effective manner (but you can still do pretty good
job with the .NET SDK alone). Application source code goes through two distinct steps
before a user can run it. First, the source code is compiled to Microsoft Intermediate
Language (MSIL) code using a .NET compiler. Then, at runtime, the MSIL code is
compiled to native code. When we debug a .NET application, this process works in
reverse. The debugger first maps the native code to the MSIL code. The MSIL code is
then mapped back to the source code using the programmer's database (PDB) file. In
order to debug an application, these two mappings must be available to the .NET runtime
environment.

COMPILER

JIT (Just–in-Time Compiler) & Debugging 



The .NET Runtime ships with a Just-In-Time (JIT or JITter) compiler, which will convert
the MSIL code in to the native code (CPU Specific executable code). So whatever code
we write will be complied in to MSIL format and the JIT takes over when you run it.

.NET LANGUAGES

Some .NET Languages

C#
COBOL
Eiffel
Fortran
Mercury
Pascal
Python
SML
Perl
Smalltalk
VB.NET
VC++.NET
J#.NET
Scheme
....
More are planned or under
development