User FAQs
If you have a question, please send it to us.
1) What is an Enumeration ?
An Enumeration provides a way of grouping symbolic constants.
The default data type is System.Int32.
The first item in an enumeration has a value of 0.
An enumeration is a value data type.
The default access modifier for an enumeration is public.
public enum enCities
{
london = 1,
tokyo = 2
}
2) Is it possible to associate a text string with each Enumeration value ?
Yes. You can amend the Description component to each value.
public enum enCities
{
[System.ComponentModel.Description("UK")]
london = 1,
[System.ComponentModel.Description("JAPAN")]
tokyo = 2
}
3) Is it possible to return the int value from an Enumeration ?
Yes. You can just cast it to an int.
int myValue = (int)enCities.london;
4) Is it possible to return the enumeration from a corresponding text string ?
Yes. You can use the enumeration Parse method.
string myCityName = "london";
enCities myEnum = (enCities)System.Enum.Parse(typeof(enCities), myCityName);
5) Is it possible to return the string name instead of the number from an Enumeration ?
Yes. You can use the enumeration GetName method.
int myValue = enCities.tokyo;
string myCityName = System.Enum.GetName(typeof(enCities), myValue);
6) Is it possible to iterate through the values defined in an Enumeration ?
Yes. Although the Enum base class does not support the IEnumerable interface.
You can however use the GetValues method which returns all the enumerated values in an array.
foreach (enCities cityenum in System.Enum.GetValues(typeof(enCities)))
{
Console.WriteLine( cityenum + " (" + (int)cityenum + ")" );
}
7) Is it possible to iterate through the names defined in an Enumeration ?
Yes.
foreach (string cityname in System.Enum.GetNames(typeof(enCities)))
{
Console.WriteLine( cityname + " (" + (int)System.Enum.Parse(typeof(enCities), cityname) + ")" );
}
8) Write code to iterate through the values defined in an Enumeration ?
First create an Extension Method that can easily return the Description.
This extension method will use reflection to get an array of the enum's members, and then from the array's first element's member info it will check and return if the member has a Description attribute defined.
If that attribute exists, then it will return the Description value, this will be contained within the GenericEnum variable's ToString() method.
public static class EnumExtensionMethods
{
public static string GetDescription(this Enum GenericEnum)
{
Type genericEnumType = GenericEnum.GetType();
MemberInfo[] memberInfo = genericEnumType.GetMember(GenericEnum.ToString());
if ((memberInfo != null && memberInfo.Length > 0))
{
var _Attribs = memberInfo[0].GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), false);
if ((_Attribs != null && _Attribs.Count() > 0))
{
return ((System.ComponentModel.DescriptionAttribute)_Attribs.ElementAt(0)).Description;
}
}
return GenericEnum.ToString();
}
}
foreach (enCities city in System.Enum.GetValue(typeof(enCities)))
{
string enumName = System.Enum.GetName(typeof(enCities), myValue);
string enumDescription = city.GetDescription();
Console.WriteLine( {0} - ( {1} ), enumName, enumDescription);
}
9) What does Strongly Typed mean ?
A programming language is strongly typed if there are no implicit type conversions.
A strongly typed programming language is one in which each data type is predefined as part of the programming language and all constants or variables must be defined with a specifc data type.
An advantage of strong data typing is that it imposes a rigorous set of rules on the programmer and therefore guarantees a certain consistency of results.
10) What is the difference between Procedural and Object Oriented programming ?
Procedural is based on a modular approach where a larger program is broken up into smaller procedures or functions.
Each procedure has a set of instructions that is executed one after another.
Access modifiers are not used in procedural programming which means that most of the code can be accessed from anywhere.
Object Oriented is based on classes which have methods and properties. Objects are then created from these classes.
Access modifiers can be used to hide implementation.
Inheritance can be used to create reusable components.
11) What is Object Oriented programming ?
This is a style of programming that is based on objects containing methods, properties and fields arranged in a hierarchy.
This type of programming is based around four key features:
Polymorphism - Create routines that can operate on objects of different types. This is handled by late binding and multiple interfaces.
Encapsulation - Hide parts of the implementation and hide complexity. This is achieved using access modifiers.
Inheritance (Interface) - (Public Inheritance) Define methods (without implementation) that must be declared in a derived class.
Inheritance (Implementation) - (Private Inheritance) Inherit method implementation from a base class.
12) How is C# different from C ?
C# is an object-oriented programming language.
C is a procedural programming language.
13) Is C# a Strongly Typed programming language ?
Yes. C# and VB.Net are both strongly typed languages.
14) Is C# an Object Oriented programming language ?
Yes. It has the four key features outlined above.
15) Can you describe the four different types of inheritance ?
Single Inheritance - contains one base class and one derived class.
Hierarchical Inheritance - contains one base class and multiple derived classes with the same base class.
Multi-level Inheritance - contains a class derived from another derived class.
Multiple Inheritance - (not supported in C#) contains a class derived from multiple base classes.
16) Can you describe how Garbage Collection works ?
The garbage collector automatically gets information about unreferenced objects from the .NET runtime environment and then invokes their Finalize method.
GC.Collect
17) What is the 'using' statement ?
The using block is used to obtain a resource and use it and then automatically dispose of it at the end of the block.
This allows you to not specify the data type at declaration ?
18) Write code that will resize a System.Drawing.Rectangle by reducing its height and width by 5 points ?
System.Drawing.Rectangle myRectangle = new System.Drawing.Rectangle(100,100);
myRectangle.Inflate(-5,-5);
19) What is Unit Testing ?
20) Can you give some examples of different types of test cases ?
Positive test cases - correct data, correct output
Negative test cases - incorrect / missing / invalid data, proper handling
Exception test cases - exceptions are thrown and handled properly
21) Write a For loop to display the values in a list ?
System.Collections.Generic.List<int>MyList;
for (int mynumber = 0; mynumber < MyList.Length; mynumber++)
{
MessageBox.Show(MyList[mynumber]);
}
22) Write a For-Each loop to display the values in a list ?
System.Collections.Generic.List<int>MyList;
foreach (int mynumber in MyList)
{
MessageBox.Show(mynumber);
}
23) Describe the 'return' statement and explain where it can be used ?
return - terminates execution of the method in which it appears and returns control to the calling method.
24) What is the difference between 'break' and 'continue' inside a for loop ?
break - breaks out of the loop completely.
continue - continues to execute the next iteration.
for (int i = 0; i < 10; i++)
{
if (i == 0)
{
continue; // the for-loop will jump to the next loop
}
DoSomeThing(i);
if (i == 5)
{
break; // the for-loop with exit
}
}
25) Write a Switch statement to match against two conditions ?
switch (myNumber)
{
case < 10:
break;
case >= 10:
break;
default:
break;
}
© 2023 Better Solutions Limited. All Rights Reserved. © 2023 Better Solutions Limited TopPrevNext