C# Avançado
C# Avançado
years Experience
© Copyright by Interviewbit
Contents
4. What is a delegate?
A delegate is a reference type entity that is used to store the reference of a method.
So whenever you need to call a method, you can easily use the delegate by initializing
a pointer to that method, thereby helping to implement Encapsulation.
// Main Method
public static void Main(String []args)
{
Delegates in C# are similar to function pointers in C/C++ and can be used to pass
methods as arguments to other methods, chained together, or even can be used to
call methods on events.
8. What is a namespace?
Namespace in C# can be considered as a container in which you can define classes,
methods, interfaces, structures, or other child namespaces, such that classes with
the same name but different namespaces won’t cause any error. In C#, namespaces
are an efficient entity to organize codes for larger applications.
The major advantages of Namespace are:
Namespaces help in effectively organizing large C# code projects.
To use any entity in a namespace, simply use <namespace name>.<entity name>
No two classes with the same name in a different namespace will cause any
error.
private constructor_name
{
// Code
}
using System;
public class SingletonDemo
{
private static string CreatedOn;
private static SingletonDemo instance = null;
private SingletonDemo()
{
CreatedOn = DateTime.Now.ToLongTimeString();
}
// Method in which ref parameter is passed and a value is written into this variable
public static void initializeString(ref string str1)
{
}
}
Hello Scaler
Hello InterviewBit
Whereas out keyword is used to pass a variable as an empty container that can
store multiple values to a method as a reference type. out keyword allows uni-
directional data passing, as the container passed using out keyword doesn’t
need to be initialized beforehand.
Example 2: Let’s see if and how the data flows when out keyword is used:
string str1;
// Pass variable str1 to the method using out keyword
// Normally it should pass the default value, but due to use of out keyword
// An empty container will be passed instead, leading to error
checkIfScaler(out str1);
// Lets try again with another value
checkIfScaler(out str2);
}
if (str1 == "Scaler") {
Console.WriteLine("Hello!!Scaler");
}
Example 3: Now let us try to see what happens when we initialize some value to an
out parameter.
// Method in which out parameter is passed and a value is written into this variable
public static void initializeString(out string str1)
{
str1 = "InterviewBit";
}
}
4
4
4
4
Explanation:
The correct value of the above code will be “4 4 4 4” and not “1 2 3 4”. Let us see why.
We are creating a delegate of functions using statement
foreach(var v in values)
funcs.Add( ()=>v );
So the loop will traverse for all values as v, i.e. 1, 2, 3, till 4. Now when ()=>v is used, it
means to return the latest value of variable v, and not the value when delegate was
created. Therefore when the methods run, the last value assigned to v was 4, so the
methods will be called using value 4 each time, and hence the output will be
“4
4
4
4”
<?xml version=”1.0”?>
<configuration>
<system.web>
<compilation targetFramework=4.0>
</configuration>
class Car()
{
public Car()
{
CarName="default car name";
}
public string CarName{get;set;}
}
For C# 6.0:
class ABC
{
// Declare the attribute
private int id;
// Declare id’s property
public int ID
{
// read-only getter property
get { return id; }
set { id = value; }
}
}
Struct Class
Syntax
public void dispose( ){ protected v
// Dispose code here // finaliza
} }
Purpose finalize() me
dispose() method is used
free the unm
to free unmanaged
resources wh
resources upon invoking.
the garbage
Performance finalize() me
dispose() method
comparison
happens instantly and
method and
hence is faster.
performance
To access any element, all you need to do is just pass the value of the element
alongside enum’s variable, such as:
using System;
using System.Collections;
using System.Threading;
namespace MutexDemo
{
class MutexDemoClass
{
private static Mutex mutex = new Mutex();
private const int countOfHits = 1;
private const int countOfThreads = 3;
private static void ThreadProcess()
{
for (int i = 0; i < countOfHits; i++)
{
showMutexDemo();
}
}
// Driver code
static void Main(string[] args)
{
for (int i = 0; i < countOfThreads; i++)
{
Thread thread = new Thread(new ThreadStart(ThreadProcess));
thread.Name = String.Format("Thread{0}", i + 1);
thread.Start();
}
}
}
}
The above code is a simple example to show how Mutex locks a resource and only
that thread can release the Mutex.
using System;
// Sealed class
sealed class SealedClass
{
public int Add(int x, int y)
{
return x + y;
}
}
class Class1: SealedClass
{
static void Main(string[] args)
{
SealedClass sealedCls = new SealedClass();
int total = sealedCls.Add(4, 5);
Console.WriteLine("Total = " + total.ToString());
}
}
Compilation error:
prog.cs(12,7): error CS0509: `Class1': cannot derive from sealed type `SealedClass'
prog.cs(4,14): (Location of the symbol related to previous error)
Compilation failed: 1 error(s), 0 warnings
Conclusion:
If you are planning to go for an interview in C# with 5+ years of experience, then you
will need to be ready for some aggressive, in-depth, and out-of-the-box questions.
However, with proper knowledge, brush up on your experience and expertise, and
proper preparation, you can easily crack any interview.
Useful Resources:
OOPs Interview Questions
.NET Interview Questions
ASP.NET Interview Questions
MVC Interview Questions
Css Interview Questions Laravel Interview Questions Asp Net Interview Questions