Sunday, February 5, 2012

Disscuss access specifier in c#


Access modifier specify the accessibility of a variable or and object. That means who can access the object and how can it be accessed.which controls whether they can be used from other code in your assembly or other assemblies. You can use the following access modifiers to specify the accessibility of a type or member when you declare it:

public : 
The type or member can be accessed by any other code in the same assembly or another assembly that references it.

Saturday, February 4, 2012

Write a code to get file name from file path in c#


  String filePath = @"c:\file\neme\file.txt";

=> filePath.Split('\\')[filePath.Split('\\').Length-1]     ---->   Will give file name with extension

=> (filePath.Split('\\')[filePath.Split('\\').Length-1]).Split('.')[0] --> Will give file name without extension 


=> filePath.Split('\\')[0]  --> Will give drive letter 


What are Delegates?



A delegate is a form of type-safe function pointer used by the .NET Framework. Delegates specify a method to call and optionally an object to call the method on. They are used, among other things, to implement callbacks and event listeners. It encapsulates a reference of a method inside a delegate object. The delegate object can then be passed to code which can call the referenced method, without having to know at compile time which method will be invoked.

Basically it is similar like the old "C" age function pointer, where functions can be assigned like a variable and called in the run time based on dynamic conditions. C# delegate is the smarter version of function pointer which helps software architects a lot, specially while utilizing design patterns.

At first, a delegate is defined with a specific signature (return type, parameter type and order etc). To invoke a delegate object, one or more methods are required with the EXACT same signature. A delegate object is first created similar like a class object created. The delegate object will basically hold a reference of a function. The function will then can be called via the delegate object.



Syntax :

How a thread is created ?

First, create a new Thread Start delegate. The delegate points to a method that will be executed by the new thread. Pass this delegate as a parameter when creating a new Thread instance. Finally, call the Thread.Start method to run your method.


In Detail :


1. Create a System.Threading.Thread object.
2. Create the call back function
3. Starting the Thread




Thread newThread= new Thread(new ThreadStart (MyCallbackFunction));


                 /  \                                                      /  \
                   |         (1)                                            |          (2)




newThread.Start()


        /  \
          |        (3)







What is thread join?

Stops of prevents the calling thread until a called thread terminates, and side by side continuing to perform standard COM and SendMessage pumping.


Syntax of this method :


// create a new thread object from thread class
Thread threadToJoin  =  new Thread(threadmethod); 


//Thread have come to running state.
threadToJoin.Start();   


 // Wait for foreground thread to end.
threadToJoin.Join()
^
 |   ____ so this threadToJoin thread will wait, until other threads are terminated.


Example : Suppose you are cooking something, and in between you need something to finish cooking, so you send your servant to get that, so you will call your cooking thread .join to wait till the servant thread not finished/ servant does not come with required item, and when servant arrives with the item servant thread terminates and your thread can continue.
               
void threadmethod()
{
    //method body
}




Live

Your Ad Here