Sei sulla pagina 1di 4

String Reverse Program:

class StringReverseProgram { static void Main(string[] args) { string normalStr = "This is DotNetMirror"; string reverseStr = string.Empty; foreach (char chr in normalStr) { reverseStr = chr + reverseStr; } Console.WriteLine(reverseStr); //Output : rorriMteNtoD si sihT Console.ReadKey(); } }

Remove null/blank values from an array using C#.NET


string[] myColors = { "Red", "Blue", "", "Green", "", null, "pink" }; List<string> tempListColors = new List<string>(); foreach (string color in myColors) { if (!string.IsNullOrEmpty(color)) tempListColors.Add(color); } myColors = tempListColors.ToArray();

.NET program to check the number is palindrome or not


class PalindromeNumberOrNot { public static void Main() { Console.WriteLine("********** Palindrome Number Check Example ********"); Console.WriteLine("Enter Number to Want to check"); int numberToCheck = int.Parse(Console.ReadLine()); int lenthOfNumber = numberToCheck.ToString().Length; double reminder = 0; double sum = 0; int tempNo = numberToCheck; while (tempNo > 0) { reminder = tempNo % 10; sum = sum * 10 + reminder; tempNo = tempNo / 10; }

if (sum == numberToCheck) { Console.WriteLine("The given Number {0} is a Palindrome Number", numberToCheck); } else { Console.WriteLine("The given Number {0} is NOT a Palindrome Number", numberToCheck); } } }

Implementing Singleton class in .NET Definition:


A class which can be instantiated only once is called singleton class. The term singleton is derived from the mathematics concept of a singleton. Singleton is set with only one element. E.g.: Set {0} is a singleton.

How to achieve singleton class?


Mark the constructor as private so that outside the class its not possible to create instance of the class. Create static/Shared method can be added to the class to create instance of the class and return the reference of the object. class SingletonClass { private static SingletonClass obj; public int tempVal { get; set; } //private constuctor private SingletonClass() { } public static SingletonClass GetInstance() { if (obj == null) { obj = new SingletonClass(); } return obj; } }

class SingletonClassProgram { public static void Main(string[] args) { SingletonClass obj1,obj2; //obj1=new SingletonClass(); //invlid due to constructore is private. Error as inaccissible due to protection level obj1=SingletonClass.GetInstance(); obj1.tempVal =20; obj2=SingletonClass.GetInstance(); Console.WriteLine(" /********* singleton class Example **********/"); Console.WriteLine("variable value is {0}",obj2.tempVal); //value is 20 because both obj1,obj2 are referring to same object Console.Read(); } }

Reverse only the word in the string not the whole string without using inbuilt functions
class Palindrome { public static void Main() { string text = "C Sharp Corner "; Console.WriteLine(PrintWordInReverse(text)); } //Method to reverse the order static string PrintWordInReverse(string str) { string revWord = string.Empty; string finalsentence = string.Empty; for (int i = 0; i < str.Length; i++) { if(str[i]!=null) { revWord=str[i]+revWord; } else { revWord=revWord+""; finalsentence=finalsentence+revWord; revWord=string.Empty;

} } if (revWord != string.Empty) { } return finalsentence; } }

Potrebbero piacerti anche