The environment allows you to define your variables name, value and type. At runtime the environment values are not allowed to modify.
Declared the environment variables using the <env-entry> tag in the deployment descriptor (WEB-INF/web.xml in Tomcat). The elements of the <env-entry> tag are:
* <description>: an optional description.
* <env-entry-name>: the entry name.
* <env-entry-value>: a value.
* <env-entry-type>: the java variable type(Boolean, Byte, Double, Character, Float, Integer, Long, Short, String).
Monday, September 27, 2010
Saturday, September 18, 2010
C# - Program Structure, Data Types, Type Casting (Conversion)
Program Structure
//the using keyword is used to include the System namespace in the program.
using System;
//the namespace declaration. A namespace is a collection of classes. The HelloWorldApplication namespace contains the class HelloWorld.
namespace HelloWorldApplication
{
//a class declaration, the class HelloWorld contains the data and methods
class HelloWorld
{
// the mehod declaration
void Print()
{
//Comments
/*...*/
//
}
}
}
C# is case sensitive.
All statements and expression must end with a semicolon (;).
Data Types
Value types
- value type variables can be assigned a value directly.
- bool, byte, char, decimal, double, float, int, long, sbyte, short, uint, ulong, ushort
- get the exact size of a type or a variable - sizeof(int)
Reference types
- The reference types do not contain the actual data stored in a variable, but they contain a reference to the variables. in other words, they refer to a memory location.
- Using multiple variables, the reference types can refer to a memory location. If the data in the memory location is changed by one of the variables, the other variable automatically reflects this change in value.
- object, dynamic, and String.
- object obj = 100; //it will assign ar comiple time
- dynamic d = 100; //it will assign at run time
- String s ="C# code"; or @"c# code";
Pointer types (Unsafe Codes)
- store the memory address of another type.
- char* cptr;
Type Conversion (Type Casting) - converting one type of data to another type, 2 forms
- Implicit type conversion
- performed by in a type-safe
- int i = 123456789;
long l = il
- Derived d = new Derived();
Base b = d;
- Explicit type conversion
- conversions are done explicitly by users using the pre-defined functions. Explicit conversions require a cast operator.
- double d = 1234.67;
int i= (int)d; // 1234
- type conversion methods - ToBolean, ToByte, ToChar, ToString()
- int i = 75; i.ToString();
//the using keyword is used to include the System namespace in the program.
using System;
//the namespace declaration. A namespace is a collection of classes. The HelloWorldApplication namespace contains the class HelloWorld.
namespace HelloWorldApplication
{
//a class declaration, the class HelloWorld contains the data and methods
class HelloWorld
{
// the mehod declaration
void Print()
{
//Comments
/*...*/
//
}
}
}
C# is case sensitive.
All statements and expression must end with a semicolon (;).
Data Types
Value types
- value type variables can be assigned a value directly.
- bool, byte, char, decimal, double, float, int, long, sbyte, short, uint, ulong, ushort
- get the exact size of a type or a variable - sizeof(int)
Reference types
- The reference types do not contain the actual data stored in a variable, but they contain a reference to the variables. in other words, they refer to a memory location.
- Using multiple variables, the reference types can refer to a memory location. If the data in the memory location is changed by one of the variables, the other variable automatically reflects this change in value.
- object, dynamic, and String.
- object obj = 100; //it will assign ar comiple time
- dynamic d = 100; //it will assign at run time
- String s ="C# code"; or @"c# code";
Pointer types (Unsafe Codes)
- store the memory address of another type.
- char* cptr;
Type Conversion (Type Casting) - converting one type of data to another type, 2 forms
- Implicit type conversion
- performed by in a type-safe
- int i = 123456789;
long l = il
- Derived d = new Derived();
Base b = d;
- Explicit type conversion
- conversions are done explicitly by users using the pre-defined functions. Explicit conversions require a cast operator.
- double d = 1234.67;
int i= (int)d; // 1234
- type conversion methods - ToBolean, ToByte, ToChar, ToString()
- int i = 75; i.ToString();
C# - System.Reflection
System.Reflection - Reflection objects are used for obtaining type information (attribute, assembly, late binding methods & properties at run time.
namespace UnitTestVM
{
[TestClass]
public class ReflectionClass
{
public static int Id;
public static string Name;
public static void ReflectionMethod()
{
Type type = typeof(ReflectionClass); // type pointer
Debug.WriteLine("Fields Info");
FieldInfo[] fields = type.GetFields(); // Obtain all fields
foreach (var field in fields) // Loop through fields
{
string name = field.Name; // attribute name
object obj = field.GetValue(null); // attribute value
System.Type typ = obj.GetType(); //attribute type
Debug.WriteLine(name + " = " + obj + ", " + typ );
}
Debug.WriteLine("Assembly Info");
System.Reflection.Assembly info = typeof(System.Int32).Assembly;
Debug.WriteLine(info); //get assembly information
Debug.WriteLine("Custom Attributes");
System.Reflection.MemberInfo member = typeof(ReflectionClass);
object[] att = member.GetCustomAttributes(true);
for (int i = 0; i < att.Length; i++) Debug.WriteLine(att[i]);
Debug.WriteLine("Method Info");
MethodInfo[] mi = type.GetMethods();
for (int i = 0; i < mi.Length; i++) Debug.WriteLine(mi[i]);
Debug.WriteLine("Member Info");
MemberInfo[] memi = type.GetMembers();
for (int i = 0; i < memi.Length; i++) Debug.WriteLine(memi[i]);
Debug.WriteLine("Property Info");
PropertyInfo[] pi = type.GetProperties();
for (int i = 0; i < pi.Length; i++) Debug.WriteLine(pi[i]);
}
}
[TestClass]
public class SOAPTest
{
[TestMethod]
public void ReflectionTestMethod()
{
ReflectionClass.Id = 123;
ReflectionClass.Name = "C#";
ReflectionClass.ReflectionMethod(); // Invoke reflection methods
}
}
}
Fields Info
Id = 123, System.Int32
Name = C#, System.String
Assembly Info
mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
Custom Attributes
Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttribute
Method Info
Void ReflectionMethod()
System.String ToString()
Boolean Equals(System.Object)
Int32 GetHashCode()
System.Type GetType()
Member Info
Void ReflectionMethod()
System.String ToString()
Boolean Equals(System.Object)
Int32 GetHashCode()
System.Type GetType()
Void .ctor()
Int32 Id
System.String Name
Property Info
namespace UnitTestVM
{
[TestClass]
public class ReflectionClass
{
public static int Id;
public static string Name;
public static void ReflectionMethod()
{
Type type = typeof(ReflectionClass); // type pointer
Debug.WriteLine("Fields Info");
FieldInfo[] fields = type.GetFields(); // Obtain all fields
foreach (var field in fields) // Loop through fields
{
string name = field.Name; // attribute name
object obj = field.GetValue(null); // attribute value
System.Type typ = obj.GetType(); //attribute type
Debug.WriteLine(name + " = " + obj + ", " + typ );
}
Debug.WriteLine("Assembly Info");
System.Reflection.Assembly info = typeof(System.Int32).Assembly;
Debug.WriteLine(info); //get assembly information
Debug.WriteLine("Custom Attributes");
System.Reflection.MemberInfo member = typeof(ReflectionClass);
object[] att = member.GetCustomAttributes(true);
for (int i = 0; i < att.Length; i++) Debug.WriteLine(att[i]);
Debug.WriteLine("Method Info");
MethodInfo[] mi = type.GetMethods();
for (int i = 0; i < mi.Length; i++) Debug.WriteLine(mi[i]);
Debug.WriteLine("Member Info");
MemberInfo[] memi = type.GetMembers();
for (int i = 0; i < memi.Length; i++) Debug.WriteLine(memi[i]);
Debug.WriteLine("Property Info");
PropertyInfo[] pi = type.GetProperties();
for (int i = 0; i < pi.Length; i++) Debug.WriteLine(pi[i]);
}
}
[TestClass]
public class SOAPTest
{
[TestMethod]
public void ReflectionTestMethod()
{
ReflectionClass.Id = 123;
ReflectionClass.Name = "C#";
ReflectionClass.ReflectionMethod(); // Invoke reflection methods
}
}
}
Id = 123, System.Int32
Name = C#, System.String
Assembly Info
mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
Custom Attributes
Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttribute
Method Info
Void ReflectionMethod()
System.String ToString()
Boolean Equals(System.Object)
Int32 GetHashCode()
System.Type GetType()
Member Info
Void ReflectionMethod()
System.String ToString()
Boolean Equals(System.Object)
Int32 GetHashCode()
System.Type GetType()
Void .ctor()
Int32 Id
System.String Name
Property Info
Friday, September 10, 2010
C# - Generics, Boxing, Unboxing
Generics
- C# is a strongly typpe language, when using C# you should declare a type prior to storing data in it.
- Generics types to eliminate redundant code, type safety, code re-usability and performance.
- create generic interfaces, classes, methods, events and delegates.
- Generic can be defined by putting the <T> sign after the class or method name. instead of "T" you can use any word.
- Generic data type obtained at run-time by using reflection.
- Generics allow you to write a class or method that can work with any data type.
- System.Collection.Generic namespace contains Collection<T>, Dictionary<TKey, TValue>, List<T>, Queue<T>, Stack<T>
Type Safety Example
ArrayList obj = new ArrayList();
obj.Add(50);
obj.Add("Dog");
obj.Add(new TestClass());
foreach(int i in obj) Console.WriteLine(i);
//the code will compile, but run time iteration will through 'InvalidCastException' occurred when it print "Dog", bcz it is not integer, the code will print 50.
GenericClass<int> intObj = new GenericClass<int>();
intObj.setItem(0, 50);
intObj.setItem(1, "Dog"); //compiler error, the compiler doesn't compile the code.
Performance
Generics are faster than other collections such as ArrayList. In non-generic colloection, boxing and unboxing overhead when a value type is converted to reference type and vice-versa.
ArrayList obj = new ArrayList();
obj.Add(50); //boxing- convert value type to reference type
int x= (int)obj[0]; //unboxing
GenericClass<int>, an int type is generated dynamically from the compiler, boxing and unboxing no longer occurs.
GenericClass<int> obj = new GenericClass<int>();
obj.Add(50); //No boxing
int x= obj[0]; // No unboxing
Code reuse - A Generic class can be defined once and can be instantiated with many different types.
GenericClass<int> intObj = new GenericClass<int>();
GenericClass<char> charObj = new GenericClass<char>();
Generic Class Example
Generic Methods Example
Generic Delegates Example
Boxing - Boxing is used to store value types in the garbage-collected heap. A referece type is allocated on the heap.
int i = 123;
object o = i; //implicit conversion of a value type to the reference type (object).
stack heap
i
-----
| 23 |
-----
int i=123;
o
--- -----
| |---------> |int |
--- |----|
object o =i; |123 |
-----
Unboxing - an explicit conversion from the reference type(object) to a value type. A value type is allocated on the stack
int j = int(o);
j
-----
| 23 |
-----
int j =(int)o;
- C# is a strongly typpe language, when using C# you should declare a type prior to storing data in it.
- Generics types to eliminate redundant code, type safety, code re-usability and performance.
- create generic interfaces, classes, methods, events and delegates.
- Generic can be defined by putting the <T> sign after the class or method name. instead of "T" you can use any word.
- Generic data type obtained at run-time by using reflection.
- Generics allow you to write a class or method that can work with any data type.
- System.Collection.Generic namespace contains Collection<T>, Dictionary<TKey, TValue>, List<T>, Queue<T>, Stack<T>
Type Safety Example
ArrayList obj = new ArrayList();
obj.Add(50);
obj.Add("Dog");
obj.Add(new TestClass());
foreach(int i in obj) Console.WriteLine(i);
//the code will compile, but run time iteration will through 'InvalidCastException' occurred when it print "Dog", bcz it is not integer, the code will print 50.
GenericClass<int> intObj = new GenericClass<int>();
intObj.setItem(0, 50);
intObj.setItem(1, "Dog"); //compiler error, the compiler doesn't compile the code.
Performance
Generics are faster than other collections such as ArrayList. In non-generic colloection, boxing and unboxing overhead when a value type is converted to reference type and vice-versa.
ArrayList obj = new ArrayList();
obj.Add(50); //boxing- convert value type to reference type
int x= (int)obj[0]; //unboxing
GenericClass<int>, an int type is generated dynamically from the compiler, boxing and unboxing no longer occurs.
GenericClass<int> obj = new GenericClass<int>();
obj.Add(50); //No boxing
int x= obj[0]; // No unboxing
Code reuse - A Generic class can be defined once and can be instantiated with many different types.
GenericClass<int> intObj = new GenericClass<int>();
GenericClass<char> charObj = new GenericClass<char>();
Generic Class Example
using System;
using System.Collections.Generic;
namespace GenericClassApplication
{
public class GenericClass<T>
{
private T[] obj = new T[5]; // define an Array of Generic type with length 5
public T getItem(int index)
{
return obj[index];
}
public void setItem(int index, T value)
{
obj[index] = value;
}
}
class Program
{
static void Main(string[] args)
{
//instantiate generic with int
GenericClass<int> intObj = new GenericClass<int>();
for (int i = 0; i < 5; i++) intObj.setItem(i, i*2);
for (int i = 0; i < 5; i++) Console.WriteLine(intObj.getItem(i)); //0 2 4 6 8
//instantiate generic with char
GenericClass<char> charObj = new GenericClass<char>();
for (int i = 0; i < 5; i++) charObj.setItem(i, (char)(i*2));
for (int i = 0; i < 5; i++) Console.WriteLine(charObj.getItem(i));
}
}
}
using System.Collections.Generic;
namespace GenericClassApplication
{
public class GenericClass<T>
{
private T[] obj = new T[5]; // define an Array of Generic type with length 5
public T getItem(int index)
{
return obj[index];
}
public void setItem(int index, T value)
{
obj[index] = value;
}
}
class Program
{
static void Main(string[] args)
{
//instantiate generic with int
GenericClass<int> intObj = new GenericClass<int>();
for (int i = 0; i < 5; i++) intObj.setItem(i, i*2);
for (int i = 0; i < 5; i++) Console.WriteLine(intObj.getItem(i)); //0 2 4 6 8
//instantiate generic with char
GenericClass<char> charObj = new GenericClass<char>();
for (int i = 0; i < 5; i++) charObj.setItem(i, (char)(i*2));
for (int i = 0; i < 5; i++) Console.WriteLine(charObj.getItem(i));
}
}
}
Generic Methods Example
using System;
using System.Collections.Generic;
namespace GenericMethodApplication
{
class Program
{
static void Swap<T>(ref T a, ref T b)
{
T temp;
temp = a;
a = b;
b = temp;
}
static void Main(string[] args)
{
int a=10, b=20; //a = 10, b = 20
Swap<int>(ref a, ref b); //a = 20, b = 10
char i = 'I', j ='J';
Swap<char>(ref i, ref j); //i = 'J', j = 'I'
}
}
}
using System.Collections.Generic;
namespace GenericMethodApplication
{
class Program
{
static void Swap<T>(ref T a, ref T b)
{
T temp;
temp = a;
a = b;
b = temp;
}
static void Main(string[] args)
{
int a=10, b=20; //a = 10, b = 20
Swap<int>(ref a, ref b); //a = 20, b = 10
char i = 'I', j ='J';
Swap<char>(ref i, ref j); //i = 'J', j = 'I'
}
}
}
Generic Delegates Example
using System;
using System.Collections.Generic;
delegate T GenericDelegate<T>(T n);
namespace GenericDelegateApplication
{
class DelegateClass
{
static int n = 10;
public static int Sum(int i)
{
n += i;
return n;
}
public static int Times(int i)
{
n *= i;
return n;
}
public static int getValue()
{
return n;
}
static void Main(string[] args)
{
//create delegate instances
GenericDelegate<int> gd1 = new GenericDelegate<int>(Sum);
GenericDelegate<int> gd2 = new GenericDelegate<int>(Times);
gd1(5); //calling the methods using the delegate objects,
getValue(); //15
gd2(2);
getValue(); //30
}
}
}
using System.Collections.Generic;
delegate T GenericDelegate<T>(T n);
namespace GenericDelegateApplication
{
class DelegateClass
{
static int n = 10;
public static int Sum(int i)
{
n += i;
return n;
}
public static int Times(int i)
{
n *= i;
return n;
}
public static int getValue()
{
return n;
}
static void Main(string[] args)
{
//create delegate instances
GenericDelegate<int> gd1 = new GenericDelegate<int>(Sum);
GenericDelegate<int> gd2 = new GenericDelegate<int>(Times);
gd1(5); //calling the methods using the delegate objects,
getValue(); //15
gd2(2);
getValue(); //30
}
}
}
Boxing - Boxing is used to store value types in the garbage-collected heap. A referece type is allocated on the heap.
int i = 123;
object o = i; //implicit conversion of a value type to the reference type (object).
stack heap
i
-----
| 23 |
-----
int i=123;
o
--- -----
| |---------> |int |
--- |----|
object o =i; |123 |
-----
Unboxing - an explicit conversion from the reference type(object) to a value type. A value type is allocated on the stack
int j = int(o);
j
-----
| 23 |
-----
int j =(int)o;
Subscribe to:
Posts (Atom)