Today I am sharing with some unique but popular codes which are still being ask in interviews for fresh graduates. First I want to give you the list of those codes. (This is final release, perhaps it would grow with passage of time and technology)
- Calculate Age from DOB
- Is string contains integer using Regular Expression
- Auto-Number generation of specific length
- Totally reverse the string
- Generate Year and Month
- Out Parameter
Calculate Age from DOB
C#.Net Code
static int CalculateAge(DateTime BirthDate)
{
int YearsPassed = DateTime.Now.Year - BirthDate.Year;
if (DateTime.Now.Month < BirthDate.Month || (DateTime.Now.Month == BirthDate.Month && DateTime.Now.Day < BirthDate.Day))
{
YearsPassed--;
}
return YearsPassed;
}
VB.NET Code
Private Shared Function CalculateAge(BirthDate As DateTime) As Integer
Dim YearsPassed As Integer = DateTime.Now.Year - BirthDate.Year
If DateTime.Now.Month < BirthDate.Month OrElse (DateTime.Now.Month = BirthDate.Month AndAlso DateTime.Now.Day < BirthDate.Day) Then
YearsPassed -= 1
End If
Return YearsPassed
End Function
Is string contains integer using Regular Expression
C#.Net Code
string value = "abc123";
bool testValue = Regex.IsMatch(value, @"\d");
if (testValue)
{
Console.WriteLine("Passed");
}
else
{
Console.WriteLine("Failed");
}
VB.NET Code
Dim value As String = "abc123"
Dim testValue As Boolean = Regex.IsMatch(value, "\d")
If testValue Then
Console.WriteLine("Passed")
Else
Console.WriteLine("Failed")
End If
Auto-Number generation of specific length
C#.Net Code
string uniqueId = "000000";
int newNumber = Convert.ToInt32(uniqueId) + 1;
uniqueId = newNumber.ToString("000000");
Console.WriteLine(uniqueId);
VB.NET Code
Dim uniqueId As String = "000000"
Dim newNumber As Integer = Convert.ToInt32(uniqueId) + 1
uniqueId = newNumber.ToString("000000")
Console.WriteLine(uniqueId)
Totally reverse the string
C#.Net Code
var input = "hello world";
string output = "";
for (int i = input.Length - 1; i >= 0; i--)
{
output += input[i];
}
Console.WriteLine(output);
VB.NET Code
Dim input = "hello world"
Dim output As String = ""
For i As Integer = input.Length - 1 To 0 Step -1
output += input(i)
Next
Console.WriteLine(output)
Generate Year and Month
C#.Net Code
for (int i = DateTime.Now.Year; i >= 1990; i--)
{
Console.WriteLine(i);
}
for (int i = 1; i <= 12; i++)
{
Console.WriteLine(Convert.ToDateTime(i + "/1/2012").ToString("MMMM"));
}
VB.NET Code
For i As Integer = DateTime.Now.Year To 1990 Step -1
Console.WriteLine(i)
Next
For i As Integer = 1 To 12
Console.WriteLine(Convert.ToDateTime(i + "/1/2012").ToString("MMMM"))
Next
Out Parameter
C#.Net Code
var seq = new List<string> { "1", "Blah", "3" };
int temp = 0;
var nums =
from item in seq
let success = int.TryParse(item, out temp)
orderby item
select success ? temp : 0;
foreach (var num in nums)
{
Console.WriteLine(num);
}
VB.NET Code
Dim seq = New List(Of String)() With { _
"1", _
"Blah", _
"3" _
}
Dim temp As Integer = 0
Dim nums = _
Let success = Integer.TryParse(item, temp) _
Order By item
For Each num As var In nums
Console.WriteLine(num)
Next
No comments:
Post a Comment