Search This Blog

Monday, October 28, 2013

C# Interview Questions: It is possible to inherit in C# a base class written in VB.net?

C# > Interview Questions

Question:
   It is possible to inherit in C# a base class written in VB.net?

Answer:
   Yes, both are CLS-Compilant






C# Interview Questions: How can prevent a class from being inherited?

C# Interview Questions

Question:

How can prevent a class from being inherited?

Answer:
 
Using keyword sealed





What is the default access modifier for interface members?

C# Interview Questions

Question:

What is the default access modifier for interface members?

Answer:

Public





.NET Framework

The .NET Framework is a development framework build by Microsoft for building apps for Windows, Windows Phone, Windows Server, and Windows Azure.

Components
  • common language runtime (CLR)
  • .NET Framework class library
The .NET Framework is a managed execution environment.





NET Framework Garbage Collector

.NET Framework > Garbage Collector

The .NET Framework garbage collector manages the allocation and release of memory for applications.
When you create a new object the CLR allocates memory for the object in a managed heap. 
The runtime continues to allocate space for new objects.
Because the memory is not infinite the garbage collector performs a collection in order to free some memory. 

Advantages:
  • don't have to free memory
  • effectively allocate objects  on the managed heap
  • clears memory of the objects no longer being used
  •  memory safety

Friday, October 25, 2013

Float type in C#

C# > Types > Float

Float is a simple type that stores 32-bit floating-point values.

By default, a float on the right side of the assignment operator is treated as double.
If you do not use the suffix F, you will get a compilation error because you are trying to store a double value into a float variable:

float x = 7.5; // Error Literal of type double cannot be implicitly converted to type 'float'; use an 'F' suffix to create a literal of this type

To initialize a float variable, use the suffix f or F:

float x = 7.5F; // Ok





Char type in C #

Types in C# > Char

Char type in C# declares a Unicode character.
Unicode characters are 16-bit characters that are used to represent most of the known written languages throughout the world.

Example of char:

1.
char[] chs = new char[2];
chs[0] = 'U'; // Character literal
chs[1] = (char)85; // Cast from integral type
foreach (char c in chs)
   MessageBox.Show(c.ToString());

2. Null Char




Thursday, October 24, 2013

Bool Type C Sharp

C# > Types > Bool

bool type in C# it is used to declare variables to store the Boolean values, true and false.

Examples:

bool b = true;
MessageBox.Show(b.ToString()); // Display True

b = null; //Error: Cannot convert null to 'bool' because it is a non-nullable value type

If you need a Boolean variable that can also have a value of null, use bool?

bool? b = true;
b = null; //Ok






Types in C#

C# > Types





Difference between Value types and Reference types

Value types
Reference types
Variable content
Value
Reference
Stored
Stack
Heap
Assignment
Value
Reference
Initial value
0, false,’\0’
null






Ranges

Type
Range
byte
0 .. 255
sbyte
-128 .. 127
short
-32,768 .. 32,767
ushort
0 .. 65,535
int
-2,147,483,648 .. 2,147,483,647
uint
0 .. 4,294,967,295
long
-9,223,372,036,854,775,808 .. 9,223,372,036,854,775,807
ulong
0 .. 18,446,744,073,709,551,615
float
-3.402823e38 .. 3.402823e38
double
-1.79769313486232e308 .. 1.79769313486232e308
decimal
-79228162514264337593543950335 .. 79228162514264337593543950335
char
Unicode character.
string
String of Unicode characters.
bool
True or False.
object
An object.

ICloneable system interface

C# > System > Interfaces > IEquatable

The ICloneable interface enables you to provide a customized implementation that creates a copy of an existing object.

Definition:

public interface ICloneable
    {
        // Summary:
        //     Creates a new object that is a copy of the current instance.
        //
        // Returns:
        //     A new object that is a copy of this instance.
        object Clone();
    }

Example:
        public class Customer : ICloneable
        {
            public string Name { get; set; }
            public Address CustomerAddress { get; set; }
            public object Clone()
            {
                Customer newCustomer = (Customer)this.MemberwiseClone();
                newCustomer.CustomerAddress = (Address)this.CustomerAddress.Clone();
                 return newCustomer;
            }
        }
        public class Address : ICloneable
        {
            public string StreetName { get; set; }
            public int StreetNumber { get; set; }
            public object Clone()
            {
                return this.MemberwiseClone();
            }
        }

        Customer c1 = new Customer();
        c1.Name = "c1";
        Address a1 = new Address();
         a1.StreetName = "Street1";
        a1.StreetNumber = 1;
        c1.CustomerAddress = a1;

        Customer custClone = (Customer)c1.Clone();
        custClone.Name = "c2";

 




Factory Design Pattern C#

C# > Design Patterns > Factory

The factory completely abstracts the creation and initialization of the product from the client.

This indirection enables the client to focus on its role in the application without concerning itself with the details of how the product is created.

In this way the product implementation changes over time and the client remains unchanged.

Model:

Client -> (uses) -> Factory -> (creates) - > Product

 

Example:

Database factory class

                public enum DbOptions
        {
            SQLServer,
            Oracle
        }

        public interface IDatabase
        {
            string Connect();
        }

        public class clsSQlServer : IDatabase
        {
            public string Connect()
            {
                return ("You connected to SQlServer");
            }
        }
        public class clsOracle : IDatabase
        {
            public string Connect()
            {
                return ("You connected to Oracle");
            }
        }
        public class clsUnknown : IDatabase
        {
            public string Connect()
            {
                return ("Unknown database");
            }
        }

        public class DatabaseChoice // Factory class
        {
            static public IDatabase getDBObj(DbOptions db)

            {
                IDatabase objDb = null;
                if (db == DbOptions.SQLServer)
                {
                    objDb = new clsSQlServer();
                }
                else if (db == DbOptions.Oracle)
                {
                    objDb = new clsOracle();
                }

                // In the future if will use MySql only add here
                //else if (db == DbOptions.MySql)
                //{
                //    objDb = new clsMySql();
                //}
                else
                {
                    objDb = new clsUnknown();
                }
                return objDb;
            }
         }
// client

IDatabase objDb = DatabaseChoice.getDBObj(DbOptions.Oracle);
MessageBox.Show(objDb.Connect());