- C# Basic Tutorial
Visual C# Essentials eBook. Fully updated for C# 4.0 and Visual Studio 2010, this eBook contains 28 chapters of detailed information designed to provide everything necessary to gain proficiency in the C# programming language and Visual Studio development environment. The ebook begins with a detailed overview of the C# development and runtime environment including overviews of the. Learn.Net Framework Tutorial for beginners and professionals with examples on overloading, method overriding, inheritance, aggregation, base, polymorphism, sealed.
C# (pronounced see sharp, like the musical note C♯, but written with the number sign) is a general-purpose, multi-paradigm programming language encompassing static typing, strong typing, lexically scoped, imperative, declarative, functional, generic, object-oriented (class-based), and component-oriented programming disciplines. C# was developed around 2000 by Microsoft as part of its.NET. This is the ultimate guide to C# 4 and the.NET 4 framework. Updated with more coverage of intermediate and advanced features, new examples, and detailed discussions of recent language and framework additions, this book covers everything you will need to know about C# and putting it to work. You will also find in-depth reviews of various topics including traditional Windows programming. IronWebScraper - The C# Library 2020.4.0.4.3 add to watchlist send us an update buy now $ 399.00 Single Project License (Company, 1 developer, 1 year support and updates).
- C# Advanced Tutorial
- C# Useful Resources
- Selected Reading
A thread is defined as the execution path of a program. Each thread defines a unique flow of control. If your application involves complicated and time consuming operations, then it is often helpful to set different execution paths or threads, with each thread performing a particular job.
Threads are lightweight processes. One common example of use of thread is implementation of concurrent programming by modern operating systems. Use of threads saves wastage of CPU cycle and increase efficiency of an application.
So far we wrote the programs where a single thread runs as a single process which is the running instance of the application. However, this way the application can perform one job at a time. To make it execute more than one task at a time, it could be divided into smaller threads.
Thread Life Cycle
The life cycle of a thread starts when an object of the System.Threading.Thread class is created and ends when the thread is terminated or completes execution.
Following are the various states in the life cycle of a thread −
The Unstarted State − It is the situation when the instance of the thread is created but the Start method is not called.
The Ready State − It is the situation when the thread is ready to run and waiting CPU cycle.
The Not Runnable State − A thread is not executable, when
- Sleep method has been called
- Wait method has been called
- Blocked by I/O operations
The Dead State − It is the situation when the thread completes execution or is aborted.
The Main Thread
In C#, the System.Threading.Thread class is used for working with threads. It allows creating and accessing individual threads in a multithreaded application. The first thread to be executed in a process is called the main thread.
When a C# program starts execution, the main thread is automatically created. The threads created using the Thread class are called the child threads of the main thread. You can access a thread using the CurrentThread property of the Thread class.
The following program demonstrates main thread execution −
When the above code is compiled and executed, it produces the following result −
Properties and Methods of the Thread Class
The following table shows some most commonly used properties of the Thread class −
Sr.No. | Property & Description |
---|---|
1 | CurrentContext Gets the current context in which the thread is executing. |
2 | CurrentCulture Gets or sets the culture for the current thread. |
3 | CurrentPrinciple Gets or sets the thread's current principal (for role-based security). |
4 | CurrentThread Gets the currently running thread. |
5 | CurrentUICulture Gets or sets the current culture used by the Resource Manager to look up culture-specific resources at run-time. |
6 | ExecutionContext Gets an ExecutionContext object that contains information about the various contexts of the current thread. |
7 | IsAlive Gets a value indicating the execution status of the current thread. |
8 | IsBackground Gets or sets a value indicating whether or not a thread is a background thread. |
9 | IsThreadPoolThread Gets a value indicating whether or not a thread belongs to the managed thread pool. |
10 | ManagedThreadId Gets a unique identifier for the current managed thread. |
11 | Name Gets or sets the name of the thread. |
12 | Priority Gets or sets a value indicating the scheduling priority of a thread. |
13 | ThreadState Gets a value containing the states of the current thread. |
The following table shows some of the most commonly used methods of the Thread class −
Sr.No. | Method & Description |
---|---|
1 | public void Abort() Raises a ThreadAbortException in the thread on which it is invoked, to begin the process of terminating the thread. Calling this method usually terminates the thread. |
2 | public static LocalDataStoreSlot AllocateDataSlot() Allocates an unnamed data slot on all the threads. For better performance, use fields that are marked with the ThreadStaticAttribute attribute instead. |
3 | public static LocalDataStoreSlot AllocateNamedDataSlot(string name) Allocates a named data slot on all threads. For better performance, use fields that are marked with the ThreadStaticAttribute attribute instead. |
4 | public static void BeginCriticalRegion() Notifies a host that execution is about to enter a region of code in which the effects of a thread abort or unhandled exception might jeopardize other tasks in the application domain. |
5 | public static void BeginThreadAffinity() Notifies a host that managed code is about to execute instructions that depend on the identity of the current physical operating system thread. |
6 | public static void EndCriticalRegion() Notifies a host that execution is about to enter a region of code in which the effects of a thread abort or unhandled exception are limited to the current task. |
7 | public static void EndThreadAffinity() Notifies a host that managed code has finished executing instructions that depend on the identity of the current physical operating system thread. |
8 | public static void FreeNamedDataSlot(string name) Eliminates the association between a name and a slot, for all threads in the process. For better performance, use fields that are marked with the ThreadStaticAttribute attribute instead. |
9 | public static Object GetData(LocalDataStoreSlot slot) Retrieves the value from the specified slot on the current thread, within the current thread's current domain. For better performance, use fields that are marked with the ThreadStaticAttribute attribute instead. |
10 | public static AppDomain GetDomain() Returns the current domain in which the current thread is running. |
11 | public static AppDomain GetDomainID() Returns a unique application domain identifier |
12 | public static LocalDataStoreSlot GetNamedDataSlot(string name) Looks up a named data slot. For better performance, use fields that are marked with the ThreadStaticAttribute attribute instead. |
13 | public void Interrupt() Interrupts a thread that is in the WaitSleepJoin thread state. |
14 | public void Join() Blocks the calling thread until a thread terminates, while continuing to perform standard COM and SendMessage pumping. This method has different overloaded forms. |
15 | public static void MemoryBarrier() Synchronizes memory access as follows: The processor executing the current thread cannot reorder instructions in such a way that memory accesses prior to the call to MemoryBarrier execute after memory accesses that follow the call to MemoryBarrier. |
16 | public static void ResetAbort() Cancels an Abort requested for the current thread. |
17 | public static void SetData(LocalDataStoreSlot slot, Object data) Sets the data in the specified slot on the currently running thread, for that thread's current domain. For better performance, use fields marked with the ThreadStaticAttribute attribute instead. |
18 | public void Start() Starts a thread. |
19 | public static void Sleep(int millisecondsTimeout) Makes the thread pause for a period of time. |
20 | public static void SpinWait(int iterations) Causes a thread to wait the number of times defined by the iterations parameter |
21 | public static byte VolatileRead(ref byte address) public static double VolatileRead(ref double address) public static int VolatileRead(ref int address) public static Object VolatileRead(ref Object address) Reads the value of a field. The value is the latest written by any processor in a computer, regardless of the number of processors or the state of processor cache. This method has different overloaded forms. Only some are given above. |
22 | public static void VolatileWrite(ref byte address,byte value) public static void VolatileWrite(ref double address, double value) public static void VolatileWrite(ref int address, int value) public static void VolatileWrite(ref Object address, Object value) Writes a value to a field immediately, so that the value is visible to all processors in the computer. This method has different overloaded forms. Only some are given above. |
23 | public static bool Yield() Causes the calling thread to yield execution to another thread that is ready to run on the current processor. The operating system selects the thread to yield to. |
Creating Threads
Threads are created by extending the Thread class. The extended Thread class then calls the Start() method to begin the child thread execution.
The following program demonstrates the concept −
Programming C# 4.0 Pdf File
When the above code is compiled and executed, it produces the following result −
Managing Threads
The Thread class provides various methods for managing threads.
The following example demonstrates the use of the sleep() method for making a thread pause for a specific period of time.
When the above code is compiled and executed, it produces the following result −
Destroying Threads
The Abort() method is used for destroying threads.
The runtime aborts the thread by throwing a ThreadAbortException. This exception cannot be caught, the control is sent to the finally block, if any.
The following program illustrates this −
When the above code is compiled and executed, it produces the following result −
.NET is a framework to develop software applications. It is designed and developed by Microsoft and the first beta version released in 2000.
It is used to develop applications for web, Windows, phone. Moreover, it provides a broad range of functionalities and support.
This framework contains a large number of class libraries known as Framework Class Library (FCL). The software programs written in .NET are executed in the execution environment, which is called CLR (Common Language Runtime). These are the core and essential parts of the .NET framework.
This framework provides various services like memory management, networking, security, memory management, and type-safety.
The .Net Framework supports more than 60 programming languages such as C#, F#, VB.NET, J#, VC++, JScript.NET, APL, COBOL, Perl, Oberon, ML, Pascal, Eiffel, Smalltalk, Python, Cobra, ADA, etc.
Following is the .NET framework Stack that shows the modules and components of the Framework.
The .NET Framework is composed of four main components:
- Common Language Runtime (CLR)
- Framework Class Library (FCL),
- Core Languages (WinForms, ASP.NET, and ADO.NET), and
- Other Modules (WCF, WPF, WF, Card Space, LINQ, Entity Framework, Parallel LINQ, Task Parallel Library, etc.)
Programming C# 4.0 Pdf Download
CLR (Common Language Runtime)
It is a program execution engine that loads and executes the program. It converts the program into native code. It acts as an interface between the framework and operating system. It does exception handling, memory management, and garbage collection. Moreover, it provides security, type-safety, interoperability, and portablility. A list of CLR components are given below:
FCL (Framework Class Library)
It is a standard library that is a collection of thousands of classes and used to build an application. The BCL (Base Class Library) is the core of the FCL and provides basic functionalities.
WinForms
Windows Forms is a smart client technology for the .NET Framework, a set of managed libraries that simplify common application tasks such as reading and writing to the file system.
ASP.NET
ASP.NET is a web framework designed and developed by Microsoft. It is used to develop websites, web applications, and web services. It provides a fantastic integration of HTML, CSS, and JavaScript. It was first released in January 2002.
ADO.NET
ADO.NET is a module of .Net Framework, which is used to establish a connection between application and data sources. Data sources can be such as SQL Server and XML. ADO .NET consists of classes that can be used to connect, retrieve, insert, and delete data.
WPF (Windows Presentation Foundation)
Windows Presentation Foundation (WPF) is a graphical subsystem by Microsoft for rendering user interfaces in Windows-based applications. WPF, previously known as 'Avalon', was initially released as part of .NET Framework 3.0 in 2006. WPF uses DirectX.
WCF (Windows Communication Foundation)
It is a framework for building service-oriented applications. Using WCF, you can send data as asynchronous messages from one service endpoint to another.
WF (Workflow Foundation)
Windows Workflow Foundation (WF) is a Microsoft technology that provides an API, an in-process workflow engine, and a rehostable designer to implement long-running processes as workflows within .NET applications.
LINQ (Language Integrated Query)
It is a query language, introduced in .NET 3.5 framework. It is used to make the query for data sources with C# or Visual Basics programming languages.
Entity Framework
It is an ORM based open source framework which is used to work with a database using .NET objects. It eliminates a lot of developers effort to handle the database. It is Microsoft's recommended technology to deal with the database.
Parallel LINQ
Parallel LINQ or PLINQ is a parallel implementation of LINQ to objects. It combines the simplicity and readability of LINQ and provides the power of parallel programming.
It can improve and provide fast speed to execute the LINQ query by using all available computer capabilities.
Apart from the above features and libraries, .NET includes other APIs and Model to improve and enhance the .NET framework.
In 2015, Task parallel and Task parallel libraries were added. In .NET 4.5, a task-based asynchronous model was added.
.NET Framework Index
.Net Framework
C#
C# Control Statements
C# Functions
C# Arrays
C# Objects and Classes
C# Properties
C# Inheritance
C# Polymorphism
C# Abstraction
C# Namespace
C# Strings
C# String Functions
C# Exceptions
C# File I/O
C# Collections
C# Generics
C# Delegates
C# Reflection
C# Anonymous Functions
C# Multithreading
C# Synchronization
C# New Features
C# 2.0
- Method group conversions (delegates)
C# 3.0
- Lambda expression
- Expression trees
C# 4.0
- Generic co and contravariance
- Embedded interop types ('NoPIA')
C# 5.0
C# 6.0
- Compiler-as-a-service (Roslyn)
C# 7.0
- Generalized async return types
C# 7.1
C# Programs List
C# Interview Questions
ADO.NET Tutorial
ADO.NET Interview Questions
ASP.NET Tutorial
ASP.NET Web Forms
ASP.NET Validation
ASP.NET MVC
ASP.NET Razor
ASP.NET Interview Questions