Showing posts with label csharp examples. Show all posts
Showing posts with label csharp examples. Show all posts

Monday, April 15, 2013

Collection initializer in c sharp


Collection initializers are extension of object initializers.It look like an array.To create a collection initializers ,Auto-implemented properties are required.To work with this,a generic data type called as list is required.Generic data type holds any type of data. These are similar to c++ templates and will be indicated with <>

Example:
Code in General declaration
class result
{
public int marks {set; get;}
public string sname {set; get;}
public int id {set; get;}
}
Code for btn click
{
list<result> rs= new list<result>
{
new result{marks=100,sname="siva",id=1}
new result{marks=100,id=2}
}
foreach(result r,int rs)
Messagebox.Show(r1.marks+" "+r1.sname+" "+r1.id);
}
In the above i have taken one button to done this example

Saturday, April 13, 2013

object initializer in c sharp

In this post  i would like to give an example on object initializers in c#.net.
1.Object initializers are used to initialize the instance variables automatically
2.These are bit similar to constructions syntaxically
obs:
if the class name is test
Test t=new test(101,"bhaskar") is called as constructor
Test t=new test{eno=101,ename="bhaskar"} is called as object initializers
In the above syntax eno and ename are two implemented properties
3.The main objective of object initializer is to save no of lines  in the source code

Example:
place a button on the form
Cod in GD
class emp
{
public int eno {set;get}
public int sal {set;get}
public string name {set;get}
}
Code for button click event
{
emp e1= new emp{eno=101,ename="bhaskar",sal=5000};
MessageBox.Show(e1.no+""+e1.name+""+e1.sal);

Monday, December 24, 2012

methods of thread class in c sharp


Start():- it will start thread execution[Thread is ready for execution ]
Current Thread():- Shared method it will return thread under execution.
Sleep(milliseconds):-shared method iy will halt thread excution for perticular milliseconds
suspend():-It completly halt the thread execution
Resume():-It will state thread execution which is halted using suspend method
Abort():-It will stop thread execution[Killing thread execution]

Priority[property]:- t.Priority =thread Priority.High
                                 thread Priority.Abovenormal
                                 thread Priority.Normal[Default]
                                 thread Priority.BelowNormal
                                 thread Priority.Low
Priority will specify importance/weightage for thread.The thread with high priority will give more cpu cycle

Saturday, December 22, 2012

How to create one-Dimensional array in c#.net


This post will describe the basic concepts of chsarp which is array.
String [] str=new String[4];
In the above i have created two single dimensional array for integers and string.Here the str array size is 5.So we can create five elements from str[0] ...str[4]

Initialization of array:
int[] x=new int[] {1,2,3,4,5};
while initialization of array size is not recommended

Example:A program to print the default values of an array
Place a submit button on a form and add the below code
Button click event
{
int[] x= new int[2];
for(int i=0;i<x.lenght;i++)
{
MessageBox. Show(x[i]+..);
}
}

Bel