Found 2745 Articles for Csharp

C# program to check password validity

Samual Sam
Updated on 19-Jun-2020 09:05:00

679 Views

While creating a password, you may have seen the validation requirements on a website like a password should be strong and have −Min 8 char and max 14 charOne lower caseNo white spaceOne upper caseOne special charLet us see how to check the conditions one by one −Min 8 char and max 14 charif (passwd.Length < 8 || passwd.Length > 14) return false;Atleast one lower caseif (!passwd.Any(char.IsLower)) return false;No white spaceif (passwd.Contains(" ")) return false;One upper caseif (!passwd.Any(char.IsUpper)) return false;Check for one special characterstring specialCh = @"%!@#$%^&*()?/>.

C# program to check if a string is palindrome or not

karthikeya Boyini
Updated on 19-Jun-2020 09:05:35

10K+ Views

To check if a string is palindrome or not, you need to first find the reverse of the string using −Array.reverse()After that use the equals() method to match the original string with the reversed. If the result is true, that would mean the string is Palindrome.ExampleLet us try the complete example. Here, our string is “Malayalam”, which is when reversed gives the same result.Live Demousing System; namespace palindromecheck {    class Program {       static void Main(string[] args) {          string string1, rev;          string1 = "Malayalam";          char[] ... Read More

C# program to check if a string contains any special character

Samual Sam
Updated on 19-Jun-2020 09:06:09

8K+ Views

To check if a string contains any special character, you need to use the following method −Char.IsLetterOrDigitUse it inside for loop and check or the string that has special characters.Let us say our string is −string str = "Amit$#%";Now convert the string into character array −str.ToCharArray();With that, use a for loop and to check for each character using the isLetterOrDigit() method.ExampleLet us see the complete code.Live Demousing System; namespace Demo {    class myApplication {       static void Main(string[] args) {          string str = "Amit$#%";          char[] one = str.ToCharArray();   ... Read More

C# Program to check if a number is prime or not

karthikeya Boyini
Updated on 10-Sep-2023 08:06:41

37K+ Views

To calculate whether a number is prime or not, we have used a for loop. Within that on every iteration, we use an if statement to find that the remainder is equal to 0, between the number itself.for (int i = 1; i

C# program to check for URL in a String

Samual Sam
Updated on 19-Jun-2020 08:53:45

2K+ Views

Use the StartWith() method in C# to check for URL in a String.Let us say our input string is −string input = "https://example.com/new.html";Now we need to check for www or non-www link. For this, use the if statement in C# −if (input.StartsWith("https://www.example.com") || input.StartsWith("https://example.com")) { }ExampleYou can try to run the following code to check for URL in a string.Live Demousing System; class Demo {    static void Main() {       string input = "https://example.com/new.html";       // See if input matches one of these starts.       if (input.StartsWith("https://www.example.com") || input.StartsWith("https://example.com")) {       ... Read More

C# Multiple Local Variable Declarations

karthikeya Boyini
Updated on 19-Jun-2020 08:54:19

578 Views

In C#, you can use the comma to declare more than one local variable in a statement. The following displays the same −int a = 20, b = 70, c = 40, d = 90;ExampleLet us see an example in which we are declaring multiple local variables. Below four variable is declared and initialized in the same statement.Live Demousing System; class Demo {    static void Main() {       int a = 20, b = 70, c = 40, d = 90;       Console.WriteLine("{0} {1} {2} {3}", a, b, c, d);    } }Output20 70 40 90

C# Language advantages and applications

Samual Sam
Updated on 19-Jun-2020 08:54:56

4K+ Views

C# is a modern, general-purpose, object-oriented programming language developed by Microsoft and approved by European Computer Manufacturers Association (ECMA) and International Standards Organization (ISO).C# is designed for Common Language Infrastructure (CLI), which consists of the executable code and runtime environment that allows the use of various high-level languages on different computer platforms and architectures.Advantages of C#Object-Oriented LanguageAutomatic Garbage CollectionCross PlatformBackward CompatibilityBetter Integrity and InteroperabilityApplications of C#Games using UnityWeb Applications Client-Server ApplicationsWindows Applications Applications that run on desktopsWeb Services ApplicationsConsole ApplicationsClass Libraries

C# Generics vs C++ Templates

karthikeya Boyini
Updated on 19-Jun-2020 08:56:11

336 Views

C# Generics and C++ Templates provide support for parameterized types. The following are the differences −FlexibilityC++ Templates are more flexible than C# GenericsExplicit specializationExplicit specialization is not supported by C#Type ParameterThe type parameter cannot be used as the base class for the generic type in C#C# does not allow type parameters to have default types.Run-TimeThe C++ template has a compile-time modal, whereas C# Generics is both compile and run-time. Generics have run-time support.Non-type template parametersC#Templates will not allow non-type template parameters.Partial SpecializationC# does not even support partial specialization.

C# factorial

Samual Sam
Updated on 19-Jun-2020 08:57:13

2K+ Views

To calculate factorial in C#, you can use while loop and loop through until the number is not equal to 1.Here n is the value for which you want the factorial −int res = 1; while (n != 1) {    res = res * n;    n = n - 1; }Above, let’s say we want 5! (5 factorial)For that, n=5, Loop Iteration 1 −n=5 res = res*n i.e res =5;Loop Iteration 2 −n=4 res = res*n i.e. res = 5*4 = 20Loop Iteration 3 −n=3 res = res*n i.e. res = 20*3 = 60ExampleIn this way, all the ... Read More

C# Exception Handling Best Practices

karthikeya Boyini
Updated on 19-Jun-2020 08:57:39

206 Views

The best practices for Exception Handling in C# are based on logging the exception. The log should be to logging library to keep a record of the exceptions.The following are the best practices you should follow while handling exceptions in C# −Log exceptions using log4net, NLog, and other frameworks used for the same purpose. Log exceptions to a file and send the log to various other targets like a database, email, etc.You should log every exception in your application. They help in finding the problems in the LOC.With logging exceptions, you need to also log the contextual details useful for ... Read More

Advertisements