Thursday, August 26, 2010

Using RadialGradientBrush in Silverlight

Adding a RadialGradientBrush


RadialGradientBrush elements are very similar to the LinearGradientBrush. They define a radial gradient that flows between two or more colors. You can apply them to the properties of Silverlight controls the same way that the LinearGradientBrush is applied.

To apply a RadialGradientBrush to a Silverlight control, you need to define the Radial- GradientBrush element. You can define the center of the gradient radius using the GradientOrigin property. The GradientOrigin is set on a 0 to 1 coordinate system separated by a comma.

You can also define the radius of the gradient in the X and Y planes using the RadiusX and RadiusY properties. These properties are set on a scale where 1 is the Height or Width of the Silverlight control.

Then you also need to define two or more GradientStop elements inside the Radial- GradientBrush. Each GradientStop element needs the Color property set. You can also define the offset between 0 and 1 where that color is set using the Offset property. Silverlight applies the gradient on a radius centered at the GradientOrigin and defined by the RadiusX and RadiusY properties matching the colors at each GradientStop along the way.

Eg:

< Rectangle Height="200" Width="200" >


< Rectangle.Fill >


< RadialGradientBrush >


< GradientStop Color="Blue" />


< GradientStop Color="White" Offset=".5" / >


< GradientStop Color="Blue" Offset=".9"/ >


< /RadialGradientBrush >


< /Rectangle.Fill >


< /Rectangle >



Eg:

< Rectangle Height="200" Width="200" >


< Rectangle.Fill >


< RadialGradientBrush GradientOrigin="0,0" >


< GradientStop Color="Blue"/ >


< GradientStop Color="White" Offset=".5"/ >


< GradientStop Color="Blue" Offset=".9"/ >


< /RadialGradientBrush >


< /Rectangle.Fill >


< /Rectangle >



Eg:

< Rectangle Height="200" Width="200" >


< Rectangle.Fill >


< RadialGradientBrush GradientOrigin=".5,.5" RadiusX=".6" RadiusY=".2" >


< GradientStop Color="Blue"/ >


< GradientStop Color="White" Offset=".5"/ >


< GradientStop Color="Blue" Offset=".9"/ >


< /RadialGradientBrush >


< /Rectangle.Fill >


< /Rectangle >

Friday, August 20, 2010

Introduction to LINQ (Language Integrated Query) in C#

Introduction to LINQ (Language Integrated Query)


 Language Integrated Query (LINQ) is Microsoft's new technology for powerful, general purpose data access


 LINQ is a uniform programming model for any kind of data. LINQ enables you to query and manipulate data with a consistent model that is independent from data sources.

 LINQ is just another tool for embedding SQL queries into code


 This toolset can be used to access data coming from in-memory objects (LINQ to Objects), Databases (LINQ to SQL), XML documents (LINQ to XML), a file-system, or any other source.


 Query expressions start with the keyword from, and are written using SQL-like query operators such as Select, Where, and OrderBy:


 Every query starts with a from clause and ends with either a select clause or a group clause. The reason to start with a from clause instead of a select statement, as in SQL syntax, is related to the need to provide Microsoft IntelliSense capabilities within the remaining part of the query, which makes writing conditions, selections, and any other LINQ query clauses easier. A select clause projects the result of an expression into an enumerable object. A group clause projects the result of an expression into a set of groups, based on a grouping condition, where each group is a numerable object.


 LINQ works on collections that implement IEnumerable.

Enums in C#

Enums in C#


Enums are basically a set of named constants. i.e. these are basically a group of named constants defined under a common name.
Enums are value types and are created on the stack and not on the heap.

Eg:

using System;
public enum Colors
{
red,
green,
blue,
}


public class Mainclass
{
public static void Main()
{
string color = Colors.red.ToString();
Console.WriteLine("Color is:" + color);
int id = (int)Colors.blue;
Console.WriteLine("Color value:" + id);
}
}


Each enum type has a corresponding value type called underlying type of the enum type.

Unsafe Code ( Using Pointer) in C#

Unsafe Code ( Using Pointer in C#)

C#.NET hides most of memory management, but in some cases we need direct access to the memory, then unsafe code is used.


Eg:
using System;
class Mainclass
{
public static void Main()
{
int a = 5;

unsafe
{
int* ptr;
ptr = &a;
*ptr = 20;
}
Console.WriteLine("a = " + a);
}
}



Eg:

using System;
unsafe class Mainclass
{
public static void Main()
{
int a = 5;
int* ptr;
ptr = &a;
*ptr = 20;


Console.WriteLine("a = " + a);
}
}

Thursday, August 19, 2010

Parameter Arrays in C#

Parameter array.


Parameter arrays allows us to pass a variant  number of arguments into a method. All incoming arguments will consider as a single dimensional array. Have a look on the following example.
using System;


class Mainclass
{
public static void Main()
{
Test obj = new Test();
obj.Add(10, 20);
obj.Add(10, 20, 30);
obj.Add(1, 2, 3, 4, 5, 6);
}
}


class Test
{
public void Add(params int[] ar)
{
int sum = 0;
foreach (int i in ar)
{
sum = sum + i;
}
Console.WriteLine("Sum : " + sum);
}
}

Reference Parameters in C#

Reference Parameter


These are input/output parameters; they can be used for passing value into a function as well as to get back value from a function.

using System;
class Test
{


public void Inc(ref int a) // here a is a reference parameter. It refer the same memory address of passing arguement.
{
a = a + 10;
Console.WriteLine(a);
}
}



class Mainclass
{
public static void Main()
{
int x = 10;
Test obj = new Test();
obj.Inc(ref x); // arguement x and parameter a will be using the same memory reference.
Console.WriteLine("From Main " + x);
}
}