Difference between constants and read only ?
21. How Global Assembly Cache works ?
16. Difference between thread and a process ?
23. How Exception handling done ?
24. Suppose you have 2 interface like this.
25. Advantages of Func and Action
Advantages of
Advantages of
Const variables are declared and initialized at compile time and read only are assigned at run time.
Assignments to read only variables can be done only in declaration and constructors.
Suppose you have a file object whose creation date you want to
assign it once in constructor and should not be modified, then you can
use read only.
Constant is a one time initialization through out application life time and Read only can be one time initialization for that specific instance.
Constant is a one time initialization through out application life time and Read only can be one time initialization for that specific instance.
3. What is protected internal keyword implies ?
Keyword protected means access is limited to the containing class or
types derived from containing classes. Similarly protected internal
implies containing assembly or types derived from containing classes.
For example :
public class BaseClass
{
protected internal int x = 100;
}
public class ChildClass : BaseClass
{
}
class mainClass
{
public static void main()
{
ChildClass instance1 = new ChildClass();
instace1.x = 50; //perfect execution
OutsideClass instance2 = new OutsideClass();
instace2.x = 50; //error
}
}
4. Why are strings in C# immutable?
CopyTo method copies to another array and Clone creates new array.
9. Difference between GetType and typeOf() ?
Main difference between GetType and typeOf() is typeOf is compile time and GetType is runtime.
typeOf works on classes and structures.
example : typeOf(int)
GetType works on instances.
instance.GetType();
4. Why are strings in C# immutable?
Immutable means string values cannot be changed once they have been
created. Any modification to a string value results in a completely new
string instance, thus an inefficient use of memory and extraneous
garbage collection. The mutable System.Text.StringBuilder class should
be used when string values will change.
5. Difference between Foreach and For loop ?
If you want to iterate through a collection through index, you can
use for loop. If you want to traverse all the items in collection
without the index and order criteria you can use foreach loop.
6. Difference between System.Array.CopyTo() and System.Array.Clone() ?CopyTo method copies to another array and Clone creates new array.
7. Can class exist without namesapce ?
Yes. We can have class without namespace. It will take global namespace. We can access class as global::classname.
8 . How you call a constructor from another constructor in C# ?
public MainWindow(int i)
{
}
public MainWindow(int i,int j):this(i)
{
}
8 . How you call a constructor from another constructor in C# ?
public MainWindow(int i)
{
}
public MainWindow(int i,int j):this(i)
{
}
9. Difference between GetType and typeOf() ?
Main difference between GetType and typeOf() is typeOf is compile time and GetType is runtime.
typeOf works on classes and structures.
example : typeOf(int)
GetType works on instances.
instance.GetType();
11. What are extension methods ?
Suppose we have a variable of type say x and we want to add some methods to the type we can use the extension methods.
Class and method should be static and the parameter should be of type, to which we want to add method and mark that parameter with this keyword.
If you refer above link you can get to know for string type we can add WordCount method.
MSDN Link You can also see below link :
Extension-Methods-in-NET
Suppose we have a variable of type say x and we want to add some methods to the type we can use the extension methods.
Class and method should be static and the parameter should be of type, to which we want to add method and mark that parameter with this keyword.
If you refer above link you can get to know for string type we can add WordCount method.
MSDN Link You can also see below link :
Extension-Methods-in-NET
12. Difference between hashtable and dictionary ?
This article is good enough to explain when to use dictionary and hashtable.
This article is good enough to explain when to use dictionary and hashtable.
http://www.codeproject.com/Tips/602537/Hashtable-vs-Dictionary
http://social.msdn.microsoft.com/Forums/vstudio/en-US/2f14636c-94b7-4869-a355-d1339dd81e56/when-to-use-hashtable-dictionarystringdictionary-arraylist-listt-hibriddictionary-and?forum=csharpgeneral
http://www.tutorialspoint.com/csharp/csharp_collections.htm
http://social.msdn.microsoft.com/Forums/vstudio/en-US/2f14636c-94b7-4869-a355-d1339dd81e56/when-to-use-hashtable-dictionarystringdictionary-arraylist-listt-hibriddictionary-and?forum=csharpgeneral
http://www.tutorialspoint.com/csharp/csharp_collections.htm
13. What is GAC ?
https://www.youtube.com/watch?v=3Fr2AgycXeI
http://msdn.microsoft.com/en-us/library/yf1d93sz(v=vs.110).aspx
https://www.youtube.com/watch?v=3Fr2AgycXeI
http://msdn.microsoft.com/en-us/library/yf1d93sz(v=vs.110).aspx
15. Difference between Collections and Generics ?
Advantage of Generics over Collections
http://www.dotnet-tricks.com/Tutorial/csharp/U08E301212-Difference-between-Generics-and-Collections-with-example.html
16. How Garbage collector works with generations ?
http://www.codeproject.com/Articles/5362/Garbage-Collection-in-NET-A-deeper-look-for-the-be
16. How Garbage collector works with generations ?
http://www.codeproject.com/Articles/5362/Garbage-Collection-in-NET-A-deeper-look-for-the-be
18. What is the use of params in C# ?
http://msdn.microsoft.com/en-us/library/w5zay9db.aspx
19. Shallow copy and deep copy ?
http://msdn.microsoft.com/en-us/library/system.object.memberwiseclone(v=vs.110).aspx
http://msdn.microsoft.com/en-us/library/w5zay9db.aspx
19. Shallow copy and deep copy ?
http://msdn.microsoft.com/en-us/library/system.object.memberwiseclone(v=vs.110).aspx
20. When to use Struct vs Class vs Record ?
https://medium.com/syncfusion/struct-vs-record-vs-class-in-c-36ec0142f304
20. Explain multicast delegate with example ?
20. Explain multicast delegate with example ?
21. How Global Assembly Cache works ?
16. Difference between thread and a process ?
23. How Exception handling done ?
24. Suppose you have 2 interface like this.
25. Advantages of Func and Action
Func
and Action
delegates in C# offer several advantages, particularly in the context of functional programming, code readability, and flexibility. Here are the key advantages of each:
Advantages of Func
Return Value:
Func
delegates are designed to return a value, making them ideal for operations where a result is needed. This is useful for calculations, transformations, and any method that produces a result.- Example:
Func<int, int, int> add = (x, y) => x + y; int result = add(3, 4); // result is 7
Versatility:
Func
can be defined with varying numbers of input parameters, allowing it to be used in a wide range of scenarios. It supports up to 16 parameters.- Example:
Func<int, int, int, int> addThreeNumbers = (x, y, z) => x + y + z; int result = addThreeNumbers(1, 2, 3); // result is 6
Functional Programming:
Func
delegates facilitate functional programming paradigms, enabling methods to be treated as first-class citizens. This allows for higher-order functions, where functions can be passed as arguments, returned from other functions, and assigned to variables.- Example:
Func<int, int> square = x => x * x; int result = square(5); // result is 25
LINQ Integration:
Func
is heavily used in LINQ queries, providing a concise and readable way to manipulate collections. LINQ methods likeSelect
,Where
, andAggregate
rely onFunc
delegates.- Example:
var numbers = new List<int> { 1, 2, 3, 4, 5 }; var squares = numbers.Select(x => x * x);
Advantages of Action
No Return Needed:
Action
delegates are designed for methods that do not return a value, making them ideal for operations where the outcome is not required. This is useful for logging, printing, or modifying objects.- Example:
Action<string> printMessage = message => Console.WriteLine(message); printMessage("Hello, World!"); // Output: Hello, World!
Simplicity:
Action
simplifies scenarios where the focus is purely on executing code rather than producing a result. This makes the code more readable and straightforward.- Example:
Action<int> printSquare = x => Console.WriteLine(x * x); printSquare(5); // Output: 25
Event Handling:
Action
is commonly used in event handling, where the focus is on responding to events rather than returning values. This is useful for GUI applications, where actions are performed in response to user interactions.- Example:
Action onButtonClick = () => Console.WriteLine("Button clicked!"); onButtonClick(); // Output: Button clicked!
Copy
Flexibility:
- Like
Func
,Action
can be defined with varying numbers of input parameters, making it adaptable to different needs. It supports up to 16 parameters. - Example:
Action<int, int> printSum = (x, y) => Console.WriteLine(x + y); printSum(3, 4); // Output: 7
- Like
Summary
Func
: UseFunc
when you need to perform operations that return a result. It is versatile, supports functional programming, and integrates well with LINQ.Action
: UseAction
when you need to perform operations that do not return a result. It simplifies code execution, is ideal for event handling, and is flexible with varying numbers of parameters.
Both Func
and Action
enhance code readability, maintainability, and flexibility, making them powerful tools in C# programming.
No comments:
Post a Comment