Skip to main content

Posts

Showing posts with the label C#

Password only alphanumeric no special character -MVC C#

Password matching expression. Password must be at least 4 characters, no more than 8 characters, and must include at least one upper case letter, one lower case letter, and one numeric digit. Expression:   ^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{4,8}$ public class LoginViewModel { [ Required (ErrorMessage= "Required" )]       [ Display (Name = "User name" )] public string UserName { get ; set ; } [ Required (ErrorMessage = "Required" )] [ DataType ( DataType .Password)] [ RegularExpression ( "^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$" , ErrorMessage = "Password Alphanumeric Only. Minimum length 8" )] [ Display (Name = "Password" )] public string Password { get ; set ; } }

Remove the last character in a string in -C#

You can remove last character or no of last character with below code. please try this.   static void Main( string [] arg)         {                  string temp = "My Name is Mohammad Maksudur Rahman.>" ;        var stringValue = temp.Remove(temp.Length-1);                   Console .WriteLine( "Your string value: {0}" , stringValue);                 Console .ReadLine();           stringValue = temp.Remove(temp.Length-2);           Console .WriteLine( "Your string value: {0}" , stringValue);                 C...

Get Previous Month's First and Last Date -C#

Here this code, your can get previous month's first and last day. It's simple   static void Main( string [] arg) {    var year = DateTime .Today.Year;    var month = DateTime .Today.Month;    var firstDate = new DateTime (year, month, 1).AddMonths(-1);    var lastDate = new DateTime (year, month, 1).AddDays(-1);    Console .WriteLine( "First day Previous Month: {0}" , firstDate);    Console .WriteLine( "Last day Previous Month: {0}" , lastDate);    var  lastday =  GetMonthLastDate ( 2015 , 6 );   Console .WriteLine( "Last day : {0}" , lastDate);    Console .ReadLine(); } You can use below method to get last date of any month of the any year. private DateTime GetMonthLastDate( int year, int month) {     return new DateTime (year, month, DateTime .DaysInMonth(year, month)); }

Difference Between ref and out Keywords

=> ref causes an argument to be passed by reference not by value.  It provides a reference to the source value and changes made in the method will be made to the source object =>  Arguments passed as ref must be initialized before they are passed. =>  out also causes an argument to be passed by reference.  Changes made to the parameter are also changed in the source. =>  Arguments passed using out do not have to be initialized first. =>  The called method must assign a value prior to the method return statement

When to Use a struct vs Class, Cont’d

=> structs are value types that can contain data and functions. =>  structs are value types and do not require heap allocation. =>  structs directly store their data in the struct , classes store a reference to a dynamically allocated bject. =>  structs are useful for small data structures.  =>  structs can affect performance =>  Constructors are invoked with the new operator, but that does not allocate memory on the heap  =>  A struct constructor simply returns the struct value itself (typically in a temporary location on the stack), and this value is then copied as necessary  =>  With classes, multiple variables may have a reference to the same object  =>  It is possible for operations on one variable to affect the object referenced by the other variable. =>  With structs , the variables each have their own copy of the data, and it is not possible for operations on...

What is the Difference between String and string

=>The string type is a sealed class type that inherits directly from object. Instances of the string class represent Unicode character strings. =>  Values of the string type can be written as string literals. =>  The keyword string is simply an alias for the predefined class System.String so you can use string name = “Fred”; or String name = “Fred”;. =>  Likewise you can use string.Concat () or String.Concat () =>  Use string for variable names =>  Use S tring for class methods and reference

Difference between Thread and Process

A Process is an instance of a running application. And a thread is the Execution stream of the Process. A process can have multiple Thread. When a process starts a specific memory area is allocated to it. When there are multiple threads in a process, each thread gets a memory for storing the variables in it, and plus they can access the global variables which are common for all the threads. Thread: is used to execute more than one program at a time. The process can execute a single program. Thread is a path of execution that runs on the CPU, a process is collection of threads that share the same virtual memory. A process has at least one thread of execution, and a thread always runs in a process context.

Difference Between readonly and const c#

Several times I read the difference between Readonly and const keyword in C#. But most of the time when I remember the difference, again and again, mismatch the concept ..Finlay, write simple code.      public class Diff_ConstNReadonly         {             public const int cons_Value = 2;             public readonly int readOnly_value;             public Diff_ConstNReadonly ()             {                 readOnly_value = 3;             }         } Differences: Const :       1.    Const can only be initialized at th...

What is Object Pooling (.net)

Object pooling is nothing new, it is a container that contains active object lists that are created. In a word , a container of objects that are ready for use. List of ready-to-be-used objects are keep in this pool.Whenever there is a request for a new object, the pool manager will take the request and it will be served by allocating an object from the pool.