C# Nullable DateTime

karthikeya Boyini
Updated on 23-Jun-2020 09:50:16

17K+ Views

Using the DateTime nullable type, you can assign the null literal to the DateTime type.A nullable DateTime is specified using the following question mark syntax.DateTime?The following is the code to implement Nullable Datetime.Example Live Demousing System; class Program {    static void Main() {       DateTime? dt = null;       DateFunc(dt);       dt = DateTime.Now;       DateFunc(dt);       dt = null;       Console.WriteLine(dt.GetValueOrDefault());    }    static void DateFunc(DateTime? dt) {       if (dt.HasValue) {          Console.WriteLine(dt.Value);       } else {          Console.WriteLine(0);       }    } }Output0 9/17/2018 8:27:07 AM 1/1/0001 12:00:00 AM

Date Class in C#

karthikeya Boyini
Updated on 23-Jun-2020 09:49:22

14K+ Views

To set dates in C#, use DateTime class. The DateTime value is between 12:00:00 midnight, January 1, 0001 to 11:59:59 P.M., December 31, 9999 A.D.Let’s create a DateTime object.Example Live Demousing System; class Test {    static void Main() {       DateTime dt = new DateTime(2018, 7, 24);       Console.WriteLine (dt.ToString());    } }Output7/24/2018 12:00:00 AMLet us now get the current date and time.Example Live Demousing System; class Test {    static void Main() {       Console.WriteLine (DateTime.Now.ToString());    } }Output9/17/2018 5:49:21 AMNow using the method Add(), we will add days in a date with the ... Read More

Decimal Functions in C#

Chandu yadav
Updated on 23-Jun-2020 09:44:15

273 Views

The following are some of the decimal functions in C#.Sr.No.Name & Description1Add (Decimal, Decimal)Adds two specified Decimal values.2Ceiling(Decimal)Returns the smallest integral value that is greater than or equal to the specified decimal number.3Compare (Decimal, Decimal)Compares two specified Decimal values.4CompareTo(Decimal)Compares this instance to a specified Decimal object and returns a comparison of their relative values.5CompareTo(Object)Compares this instance to a specified object and returns a comparison of their relative values.6Divide (Decimal, Decimal)Divides two specified Decimal values.7Equals(Decimal)Returns a value indicating whether this instance and a specified Decimal object represent the same value.Let us see an example of Decimal Ceiling() method in C# that returns the smallest integral value greater than or equal to ... Read More

Replace N-th Character in a String using C#

Arjun Thakur
Updated on 23-Jun-2020 09:43:40

2K+ Views

Firstly, set a string.string str1 = "Port"; Console.WriteLine("Original String: "+str1);Now convert the string into character array.char[] ch = str1.ToCharArray();Set the character you want to replace with the index of the location. To set a character at position 3rd.ch[2] = 'F';To remove nth character from a string, try the following C# code. Here, we are replacing the first character.Example Live Demousing System; using System.Collections.Generic; public class Demo {    public static void Main(string[] args) {       string str1 = "Port";       Console.WriteLine("Original String: "+str1);       char[] ch = str1.ToCharArray();       ch[0] = 'F';   ... Read More

C# Program to Remove N-th Character from a String

Samual Sam
Updated on 23-Jun-2020 09:43:17

854 Views

To remove a character, use the remove() method and set the index from where you want to delete the character.Firstly, set the string.string str1 = "Amit"; Console.WriteLine("Original String: "+str1);To delete a character at position 4.StringBuilder strBuilder = new StringBuilder(str1); strBuilder.Remove(3, 1);You can try to run the following code to remove nth character from a string.Example Live Demousing System; using System.Text; public class Demo {    public static void Main(string[] args) {       string str1 = "Amit";       Console.WriteLine("Original String: "+str1);       StringBuilder strBuilder = new StringBuilder(str1);       strBuilder.Remove(3, 1);       str1 ... Read More

C# Program to Count Words in a Given String

karthikeya Boyini
Updated on 23-Jun-2020 09:42:41

768 Views

Let’s say we want to count the number of words in the following string −str1 = "Hello World!";Now you need to loop though till string length and increment the variable count on finding “ “, , \t as shown below −if(str1[a]==' ' || str1[a]=='' || str1[a]=='\t') {    count++; }You can try to run the following code to count words in a given string in C#.Example Live Demousing System; public class Demo {    public static void Main() {       string str1;       int a, count;       str1 = "Hello World!";       a = 0;       count = 1;       while (a

Difference Between Namespace in C# and Packages in Java

Samual Sam
Updated on 23-Jun-2020 09:41:31

3K+ Views

Packages in JavaPackages are used in Java in order to prevent naming conflicts, to control access, to make searching/locating and usage of classes, interfaces, enumerations and annotations easier, etc.A namespace is designed for providing a way to keep one set of names separate from another. The class names declared in one namespace does not conflict with the same class names declared in another.Define a Package as −package package_name;Restrict the access of classes (or class members) to the classes within the same package, but in C# with namespaces you cannot achieve this.Namespace in C#A namespace is designed for providing a way ... Read More

Scope of a Public Member Variable in C#

Ankith Reddy
Updated on 23-Jun-2020 09:40:38

449 Views

Public access specifier allows a class to expose its member variables and member functions to other functions and objects. Any public member can be accessed from outside the class.In the below example the variables length and width have been declared public. Now you can even access them outside the Main() method.The variables are accessed using the instance of the class.Rectangle r = new Rectangle(); r.length = 4.5; r.width = 3.5;Let us see the complete code.ExampleUsing System; namespace RectangleApplication {    class Rectangle {       // member variables       public double length;       public double ... Read More

Difference Between IEnumerator and IEnumerable Interface in C#

karthikeya Boyini
Updated on 23-Jun-2020 09:39:54

6K+ Views

IEnumerable and IEnumerator both are interfaces in C#.IEnumerable is an interface defining a single method GetEnumerator() that returns an IEnumerator interface.This works for readonly access to a collection that implements that IEnumerable can be used with a foreach statement.IEnumerator has two methods MoveNext and Reset. It also has a property called Current.The following shows the implementation of IEnumerable and IEnumerator.Exampleclass Demo : IEnumerable, IEnumerator {    // IEnumerable method GetEnumerator()    IEnumerator IEnumerable.GetEnumerator() {       throw new NotImplementedException();    }    public object Current {       get { throw new NotImplementedException(); }    }    // ... Read More

Difference Between Static Constructor and Instance Constructor in C#

George John
Updated on 23-Jun-2020 09:38:41

825 Views

Static ConstructorA static constructor is a constructor declared using static modifier. It is the first block of code executed in a class. With that, a static constructor executes only once in the life cycle of class.Instance ConstructorInstance constructor initializes instance data. Instance constructor is called when an object of class is created.The following example shows the difference between static and instance constructor in C#.Example Live Demousing System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Difference {    class Demo {       static int val1;       int val2;       static Demo() {         ... Read More

Advertisements