0% found this document useful (0 votes)
12 views16 pages

ImportantTopics C

The document covers various programming concepts in C#, including the differences between 'ref' and 'out' parameters, abstract classes and interfaces, delegates and events, HTTP handlers and modules, nullable types, singleton patterns, using statements, serialization, and sealed classes. Each section provides definitions, key differences, examples, and explanations of how these concepts function within C#. The document serves as a comprehensive guide for understanding these fundamental programming principles.

Uploaded by

Harindra Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views16 pages

ImportantTopics C

The document covers various programming concepts in C#, including the differences between 'ref' and 'out' parameters, abstract classes and interfaces, delegates and events, HTTP handlers and modules, nullable types, singleton patterns, using statements, serialization, and sealed classes. Each section provides definitions, key differences, examples, and explanations of how these concepts function within C#. The document serves as a comprehensive guide for understanding these fundamental programming principles.

Uploaded by

Harindra Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

-------------------------- Ref / Out -- Start

Point 1 : Out and Ref helps to pass by reference.

Point 2 : Ref is two way from caller to callee and back.

Point 3 : Out is one way it sends data back from callee to caller and any data from caller is
discarded.

Example :

static void Main(string[] args)


{
int OutSideVar=20;
SomeFunction(ref / out OutSideVar)
console.WriteLine(OutSideVar);
}

static void SomeFunction(ref / out int InSideVar)


{
InSideVar=0; // We only declare when we have to pass Out
InSideVar=InSideVar +10;
}

Output for Ref : 30


Output for Out : 10

-------------------------- Ref / Out -- End

-------------------------- Difference Between An Abstract Class And An Interface -- Start


1 . An Abstract class doesn't provide full abstraction but an interface does provide full
abstraction; i.e. both a declaration and a definition is given in an abstract class but not so in an
interface.
2 . Using Abstract we cannot achieve multiple inheritance but using an Interface we can achieve
multiple inheritance.
3 . We can not declare a member field in an Interface.
4 . We can not use any access modifier i.e. public, private, protected, internal etc. because within
an interface by default everything is public.
5 . An Interface member cannot be defined using the keyword static, virtual, abstract or sealed.

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 .

• Generally, we use abstract class at the time of inheritance.


• A user must use the override keyword before the method which is declared as abstract in
child class, the abstract class is used to inherit in the child class.
• An abstract class cannot be inherited by structures.
• It can contains constructors or destructors.
• It can implement functions with non-Abstract methods.
• It cannot support multiple inheritance.
• It can’t be static

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:

// C# program to show the


// working of abstract class
using System;

// abstract class 'GeeksForGeeks'


public abstract class GeeksForGeeks {
// abstract method 'gfg()'
public abstract void gfg();

// class 'GeeksForGeeks' inherit


// in child class 'Geek1'
public class Geek1 : GeeksForGeeks
{

// abstract method 'gfg()'


// declare here with
// 'override' keyword
public override void gfg()
{
Console.WriteLine("class Geek1");
}
}

// class 'GeeksForGeeks' inherit in


// another child class 'Geek2'
public class Geek2 : GeeksForGeeks
{

// same as the previous class


public override void gfg()
{
Console.WriteLine("class Geek2");
}
}

// Driver Class
public class main_method {

// Main Method
public static void Main()
{

// 'g' is object of class


// 'GeeksForGeeks' class '
// GeeksForGeeks' cannot
// be instantiate
GeeksForGeeks g;

// instantiate class 'Geek1'


g = new Geek1();

// call 'gfg()' of class 'Geek1'


g.gfg();

// instantiate class 'Geek2'


g = new Geek2();

// call 'gfg()' of class 'Geek2'


g.gfg();

}
}

Output :

class Geek1
class Geek2

An Interface: The syntax of an Interface looks like this:

interface
{
//Interface member
}

Note

• An Interface member cannot contain code bodies.


• Type definition members are forbidden.
• Properties are defined in an interface with the help of an access block get and set,
which are permitted for the property.

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.

-------------------------- Difference Between An Abstract Class And An Interface -- End

-------------------------- Difference Between Delegates and Events in C# -- Start

Key Differences Between Delegates and Events in C#

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.

• A delegate is declared outside a class whereas, an event is declared inside a class.

• 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.

-------------------------- Difference Between Delegates and Events in C# -- End

--------------- Difference between ASP.NET HttpHandler and HttpModule-- Start

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:

Security: For authenticating a request before the request is handled.

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.

--------------- Difference between ASP.NET HttpHandler and HttpModule-- End

--------------- Nullable -- Start

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 −

< data_type> ? <variable_name> = null;

The following example demonstrates use of nullable data types −

using System;

namespace CalculatorApplication {
class NullablesAtShow {
static void Main(string[] args) {
int? num1 = null;
int? num2 = 45;

double? num3 = new double?();


double? num4 = 3.14157;

bool? boolval = new bool?();

// display the values


Console.WriteLine("Nullables at Show: {0}, {1}, {2}, {3}", num1, num2, num3, num4);
Console.WriteLine("A Nullable boolean value: {0}", boolval);
Console.ReadLine();
}
}
}
When the above code is compiled and executed, it produces the following result −

Nullables at Show: , 45, , 3.14157


A Nullable boolean value:
--------------- Nullable -- End

--------------- Singleton -- Start

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 single constructor, that is private and parameterless.

The class is sealed.

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 :

Singleton pattern can be implemented interfaces.

It can be also inherit from other classes.

It can be lazy loaded.

It has Static Initialization.

It helps to hide dependencies.

It provides a single point of access to a particular instance, so it is easy to maintain.

Singleton class vs. Static methods

A Static Class cannot be extended whereas a singleton class can be extended.


A Static Class can still have instances (unwanted instances) whereas a singleton class prevents it.

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 :

Using .NET 4's Lazy<T> type

Explanation of the following code:


If you're using .NET 4 (or higher) then you can use the System.Lazy<T> type to make the
laziness really simple.

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.

public sealed class Singleton


{
private Singleton()
{
}
private static readonly Lazy<Singleton> lazy = new Lazy<Singleton>(() => new Singleton());
public static Singleton Instance
{
get
{
return lazy.Value;
}
}
}

--------------- Singleton -- End

------- using statement -- Start

The using block is used to obtain a resource and use it and then automatically dispose of when
the execution of block completed.

Example : Database connection with using block.

------- using statement -- End


------- Serialization / Deserialization-- Start

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 Class-- Start

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

public int Add(int a, int b)

return a + b;

class Program {

// Main Method

static void Main(string[] args)

// Creating an object of Sealed Class

SealedClass slc = new SealedClass();

// Performing Addition operation

int total = slc.Add(6, 4);

Console.WriteLine("Total = " + total.ToString());

}
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.

// C# code to show restrictions

// of a Sealed Class

using System;

class Bird {

// Creating a sealed class

sealed class Test : Bird {

// Inheriting the Sealed Class

class Example : Test {

// Driver Class

class Program {

// Main Method

static void Main()

{
}

Error :prog.cs(14,7): error CS0509: `Example': cannot derive from sealed type `Test'

prog.cs(10,14): (Location of the symbol related to previous error)

------- Sealed Class-- End

------- Abstract Class-- Start

------- Abstract Class-- End

You might also like