ImportantTopics C
ImportantTopics C
Point 3 : Out is one way it sends data back from callee to caller and any data from caller is
discarded.
Example :
Abstract Class: We can not create an object of an abstract class and can call the method of
abstract class with the help of class name only .
using System;
• using System.Collections.Generic;
• using System.Linq;
• using System.Text;
• namespace ConsoleApplication4
• {
• abstract class M1
• {
• public int add(int a, int b)
• {
• return (a + b);
• }
• }
• class M2 :M1
• {
• public int mul(int a, int b)
• {
• return a * b;
• }
• }
• class test
• {
• static void Main(string[] args)
• {
• M2 ob = new M2();
• int result = ob.add(10, 20);
• Console.WriteLine("the result is {0}", result);
• Console.ReadLine();
• }
• }
• }
Output will be : 30
Example 2:
// Driver Class
public class main_method {
// Main Method
public static void Main()
{
}
}
Output :
class Geek1
class Geek2
interface
{
//Interface member
}
Note
Interface myInterface
{
int myint
{
get;
set;
}
}
Take a look in an interface example:
• using System;
• using System.Collections.Generic;
• using System.Linq;
• using System.Text;
• namespace ConsoleApplication3
• {
• interface MyInterface
• {
• void myMethod();
• }
• class MyClass : MyInterface
• {
• public static void Main()
• {
• MyClass cls = new MyClass();
• cls.myMethod();
• }
• public void myMethod()
• {
• Console.WriteLine("welcome to MCN IT SOLUTION");
• Console.ReadLine();
• }
• }
• }
After running this program we obtain the output as follows:
Note: In C#, an Interface provides only those public services declared in the interface,
whereas an abstract class provides the public services defined in an abstract class and those
members that are inherited from the abstract class's base class.
Delegate : A delegate is a way of telling C# which method to call when an event is triggered. For
example, if you click a Button on a form, the program would call a specific method. It is this
pointer that is a delegate. Delegates are good, as you can notify several methods that an event has
occurred, if you wish so.
Event : An event is a notification by the .NET framework that an action has occurred. Each
event contains information about the specific event, e.g., a mouse click would say which mouse
button was clicked where on the form.
• Delegate is an object used as a function pointer to hold the reference of a method. On the
other hand, events provide an abstraction to delegates.
• A keyword required to declare a delegate is a delegate whereas, a keyword required to
declare an event is event.
• To invoke a method using a delegate object, the method has to be referred to the delegate
object. On the other hand, to invoke a method using an event object the method has to be
referred to the event object.
• Covariance and Contravariance provide extra flexibility to the delegate objects. On the
other hand, event has no such concepts.
• Event Accessor handles the list of event handlers whereas delegate has no such concept.
• Delegates are independent on events but, events can not be created without delegate.
HTTP Handler
HTTP Handler is the process which runs in response to a HTTP request. So whenever user
requests a file it is processed by the handler based on the extension. So, custom http handlers are
created when you need to special handling based on the file name extension. Let's consider an
example to create RSS for a site. So, create a handler that generates RSS-formatted XML. Now
bind the .rss extension to the custom handler.
HTTP Modules
HTTP Modules are plugged into the life cycle of a request. So when a request is processed it is
passed through all the modules in the pipeline of the request. So generally http modules are used
for:
Statistics and Logging: Since modules are called for every request they can be used for
gathering statistics and for logging information.
Custom header: Since response can be modified, one can add custom header information to the
response.
C# provides a special data types, the nullable types, to which you can assign normal range of
values as well as null values.
For example, you can store any value from -2,147,483,648 to 2,147,483,647 or null in a
Nullable<Int32> variable. Similarly, you can assign true, false, or null in a Nullable<bool>
variable. Syntax for declaring a nullable type is as follows −
using System;
namespace CalculatorApplication {
class NullablesAtShow {
static void Main(string[] args) {
int? num1 = null;
int? num2 = 45;
Defination :
Ensures a class has only one instance and provides a global point of access to it.
A singleton is a class that only allows a single instance of itself to be created, and usually gives
simple access to that instance.
Most commonly, singletons don't allow any parameters to be specified when creating the
instance, since a second request of an instance with a different parameter could be problematic!
Implementation :
A static variable that holds a reference to the single created instance, if any.
A public static means of getting the reference to the single created instance, creating one if
necessary.
Advantages :
A Static Class cannot be initialized with a STATE (parameter), whereas a singleton class can be.
A Static class is loaded automatically by the CLR when the program or namespace containing
the class is loaded.
Example :
All you need to do is pass a delegate to the constructor that calls the Singleton constructor, which
is done most easily with a lambda expression.
It also allows you to check whether or not the instance has been created with the IsValueCreated
property.
The using block is used to obtain a resource and use it and then automatically dispose of when
the execution of block completed.
Serialization is the process of converting an object into a stream of bytes to store the object
or transmit it to memory, a database, or a file. Its main purpose is to save the state of an
object in order to be able to recreate it when needed. The reverse process is called
deserialization.
Example :
------- Serialization / Deserialization-- End
Sealed classes are used to restrict the users from inheriting the class. A class can be sealed by
using the sealed keyword. The keyword tells the compiler that the class is sealed, and
therefore, cannot be extended. No class can be derived from a sealed class.
// C# code to define
// a Sealed Class
using System;
// Sealed class
sealed class SealedClass {
// Calling Function
return a + b;
class Program {
// Main Method
}
Now, if it is tried to inherit a class from a sealed class then an error will be produced stating that
” It cannot be derived from a Sealed class.
// of a Sealed Class
using System;
class Bird {
// Driver Class
class Program {
// Main Method
{
}
Error :prog.cs(14,7): error CS0509: `Example': cannot derive from sealed type `Test'