CS101: Programming Concepts

Verified

Added on  2019/09/23

|3
|809
|435
Homework Assignment
AI Summary
This homework assignment focuses on key C# programming concepts. The solutions provided cover topics such as garbage collection, LINQ query execution, thread safety, and the appropriate use of generics. A significant portion is dedicated to explaining extension methods in C#, including their definition, syntax, advantages, and limitations. Several examples illustrate how to define and use extension methods, along with a discussion of their practical applications. The solution also clarifies the distinction between extension methods and LINQ, highlighting that the ForEach method, while often used with LINQ, is actually an extension method for the List class. The provided solutions offer a detailed understanding of these fundamental programming concepts.
Document Page
1. A) Garbage collector is responsible for managing the lifetime of an object on the heap
2. B) LINQ query expressions are always executed regardless of the caller
3. C) Provides thread safety in code
4. D) when you have a group of overloaded methods that differ only by incoming arguments, this
is your clue that generics should be used
6)
Extension Method
Extension Methods furnish the developer with the figment that the sorts can be broadened,
regardless of whether the source code isn't accessible or if the sort is fixed... what's more, with no
compelling reason to make inferred classes. In addition, those augmentations can be connected to
examples of the sorts stretched out at run-time, notwithstanding who has made those occasions.
In all actuality this is only a hallucination upheld by the compiler, however it works entirely
well.
An extension method is a static method of a static class that can be invoked like as an instance
method syntax. Extension methods are used to add new behaviors to an existing type without
altering.
In extension method "this" keyword is used with the first parameter and the first parameter will
be of type that is extended by extension method. Other parameters of extensions types can be of
any types (data type).
Example:
//defining extension method
public static class MyExtensions
{
public static int WordCount(this String str)
{
return str.Split(new char[] { ' ', '.', ',' }).Length;
}
}
class Program
{
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
public static void Main()
{
string s = "Dot Net Tricks Extension Method Example";
//calling extension method
int i = s.WordCount();
Console.WriteLine(i);
}
}
//output:
//6
Advantages of extension method
1. An extension method is defined as static method but it is called like as an instance method.
2. An extension method first parameter specifies the type of the extended object, and it is
preceded by the "this" keyword.
3. An extension method having the same name and signature like as an instance method will
never be called since it has low priority than instance method.
4. An extension method couldn't override the existing instance methods.
5. An extension method cannot be used with fields, properties or events.
6. The compiler doesn't cause an error if two extension methods with same name and
signature are defined in two different namespaces and these namespaces are included in
same class file using directives. Compiler will cause an error if you will try to call one of
them extension method.
There are some more extension method
bool TryGetMetaProperty(name, out esteem): is utilized of course to return genuine just
if the meta-property exists, and setting its incentive in the out parameter. On the off
chance that the meta-property does not exist, it just returns false without tossing a special
case.
bool HasMetaProperty(name): returns genuine if the protest it is summoned upon
conveys a meta-property with the name given, or false generally.
IMetaProperty RemoveMetaProperty(name): is utilized to expel the meta-property from
the protest it is conjured after, returning invalid in the event that it isn't found, or an
Document Page
IMetaProperty case that can be utilized to get to its substance and status. Its own
particular properties are AutoDispose, PropertyName and PropertyValue.
void ClearMetaProperties(): is utilized to evacuate all the meta-properties the protest it is
summoned upon may convey, and conceivably arranging them.
List<string> ListMetaProperties()</string>: restores a rundown, conceivably unfilled,
with the names of the meta-properties the question it is conjured upon may convey.
The ForEach strategy isn't a piece of LINQ on the grounds that it is a general augmentation
technique added to the rundown class. In any case it is an exceptionally helpful option for
minimized inline circles (that is most likely the motivation behind why many individuals feel
that it is a piece of LINQ). As the ForEach technique, it isn't a piece of LINQ, you can't utilize it
on an identification as a customary LINQ strategy since it is characterized as an expansion for
List<T> and not Enumerable<T> - in the event that you like this strategy, you should change
over your enumerable to a rundown with a specific end goal to utilize it.
chevron_up_icon
1 out of 3
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]