0% found this document useful (0 votes)
4 views6 pages

CSharp

The document explains the differences between shallow and deep copies in C#, highlighting that shallow copies share elements while deep copies duplicate them. It also covers various C# features such as extension methods, the var keyword, anonymous types, object pooling, strong names, global assembly cache, partial classes, and operator overloading. Additionally, it provides examples of array manipulation, file reading/writing, and sorting methods.

Uploaded by

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

CSharp

The document explains the differences between shallow and deep copies in C#, highlighting that shallow copies share elements while deep copies duplicate them. It also covers various C# features such as extension methods, the var keyword, anonymous types, object pooling, strong names, global assembly cache, partial classes, and operator overloading. Additionally, it provides examples of array manipulation, file reading/writing, and sorting methods.

Uploaded by

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

Diff between Shallow copy and Deep Copy

Shallow copies duplicate as little as possible.

A shallow copy of a collection is a copy of the collection structure, not the elements. With a shallow
copy, two collections now share the individual elements.

class A
{
public int a = 0;
public void display()
{
Console.WriteLine("The value of a is " + a);
}
}
class Program
{
static void Main(string[] args)
{
A ob1 = new A();
ob1.a = 10;
A ob2 = new A();
ob2 = ob1;
ob1.a = 5; // <-- If you see value of ob2.a after this line, it will be 5.

}
}

Deep copies duplicate everything.

A deep copy of a collection is two collections with all of the elements in the original collection
duplicated.

class A
{
public int a = 0;
public void display()
{
Console.WriteLine("The value of a is " + a);
}
}
class Program
{
static void Main(string[] args)
{
A ob1 = new A();
ob1.a = 10;
A ob2 = new A();
ob2.a = ob1.a;
ob1.a = 5; // <-- If you see value of ob2.a after this line, it will be 10.
}
}

What’s the difference between the System.Array.CopyTo() and


System.Array.Clone()?
The Clone() method returns a new array (a shallow copy) object containing all the elements in the
original array.
The CopyTo() method copies the elements into another existing array. Both perform a shallow copy.

How can you sort the elements of the array in descending order?
By calling Array.Sort() and then Array.Reverse() methods

Need to do by implementing without Array function.

How to convert a sentence into Title Case (Capitalize first character of every word)?

Use ToTitleCase method.


System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(&quot;dotnetfunda.com is a
very good website&quot;);

How to read a File?

1. string strContent = System.IO.File.ReadAllText(Server.MapPath("~/MyTextFile.txt"));


2. case "ForgotPassword": file = new
FileStream(HttpContext.Current.Server.MapPath("~/HTML_Mail/ForgotPassword.htm"),
FileMode.Open, FileAccess.Read);
strReader = new StreamReader(file);
str = strReader.ReadToEnd();
strReader.Close();
break;

Write a single line of code to create a text file and write contents into it.
Use following code

System.IO.File.WriteAllText(@"c:\MyTextFile.txt", "MyContents");

What is the best way to add items from an Array into ArrayList?
Use AddRange method of the ArrayList.
string[] arr = new string[] { "ram", "shyam", "mohan" };

ArrayList arrList = new ArrayList();

arrList.AddRange(arr);

What is extension method in C#?


Extension method is a new feature in C# 3.0.

Extension methods allow existing classes to be extended without relying on inheritance or


having to change the class's source code. This means that if you want to add some methods
into the existing String class you can do it quite easily.
Here's a couple of rules to consider when deciding on whether or not to use extension
methods:

* Extension methods cannot be used to override existing methods


* An extension method with the same name and signature as an instance method will not be
called
* The concept of extension methods cannot be applied to fields, properties or events
* Use extension methods sparingly....overuse can be a bad thing!

What is the use of var keyword in C#?


This is the new feature in C# 3.0. This enable us to declare a variable whose type is implicitly
inferred from the expression used to initialize the variable.

eg.
var age = 10;

Because the initialization value (10) of the variable age is integer type of age will be treated
as integer type of variable. There are few limitation of the var type of variables.

They are:
#. They can't be initialized as null.
#. They need to be declared and initialized in the same statement.
#. They can't be used as a member of the class.
#. They can’t be passed as an argument.

What is anonymous type in C#?


This is a new feature in C# 3.0. This enable use to create a type/class on-the-fly at compile
time.

For example
var emp = new { Name = "Sheo", Gender = "Male", Active = true };

When do you absolutely have to declare a class as abstract?

1. When the class itself is inherited from an abstract class, but not all base abstract methods
have been overridden.
2. When at least one of the methods in the class is abstract.

What is object pooling?

Object Pooling is something that tries to keep a pool of objects in memory to be re-used later
and hence it will reduce the load of object creation to a great extent.

Object Pool is nothing but a container of objects that are ready for use. Whenever there is a
request for a new object, the pool manager will take the request and it will be served by
allocating an object from the pool.

This concept has been better explained in following article


http://www.c-sharpcorner.com/UploadFile/vmsanthosh.chn/109042007094154AM/1.aspx

How to create a strong name?

To create a strong name key use following code


C:\>sn -k s.snk

Strong name key is used to specify the strong name used to deploy any application in the
GAC (Global Assembly Catch) in the system.

What is Global Assembly Cache?

Abbreviated as GAC, the Global Assembly Cache is a machine-wide store used to hold
assemblies that are intended to be shared by several applications on the machine. Each
computer where the common language runtime (CLR) is installed has a global assembly
cache. The global assembly cache stores the assemblies specifically designated to be shared
by several applications on the computer.

For more details visit http://www.webopedia.com/TERM/G/Global_Assembly_Cache.htm

What is Partial Class?

Partial class

Instead of defining an entire class, you can split the definition into multiple classes by using
the partial keyword. When the application is complied, the C# complier will group all the
partial classes together and treat them as a single class. There are a couple of good reasons to
use partial classes. Programmers can work on different parts of a class without needing to
share the same physical file. Also you can separate your application business logic from the
designer-generated code.

C#
Public partial class Employee

public void DoWork()

{}

Public partial class Employee

public void GoToLunch()

{}

What is operators in C# and how many types of operators


are there in c#?
C# has a large set of operators, which are symbols that specify which operations to perform
in an expression.

Arithmetic
+-*/%

Logical (boolean and bitwise)


& | ^ ! ~ && || true false

String concatenation
+

Increment, decrement
++ --

Shift
<< >>

Relational
== != < > <= >=

Assignment
= += -= *= /= %= &= |= ^= <<= >>=

Member access
.

Indexing
[]

Cast
()

Conditional
?:

Delegate concatenation and removal


+-

Object creation
new

Type information
as is sizeof typeof

Overflow exception control


checked unchecked

Indirection and Address


* -> [] &

What is operator overloading in C#?


Operator overloading is a concept that enables us to redefine (do a special job) the existing
operators so that they can be used on user defined types like classes and structs.

For more information visit:


http://aspalliance.com/1227_Understanding_Operator_Overloading_in_C.all

You might also like