Found 34488 Articles for Programming

Initialization vs Instantiation in C#

Arjun Thakur
Updated on 30-Jul-2019 22:30:23

4K+ Views

Initialization When you assign a value to a variable when it is declared, it is called Initialization. Here is an example − int val = 50; For array initialization, you may need a new keyword, whereas to initialize a variable, you do not need it. Instantiation When you create a new object in C# for a class using the new keyword, then it is called instantiation. Use the new operator to instantiate a class in C#. Here is an example showing two objects of Student class created using new keyword − Student s1 = new Student(); Student s2 = new Student();

How to truncate a file in C#?

Ankith Reddy
Updated on 22-Jun-2020 12:34:29

702 Views

To truncate a file in C#, use the FileStream.SetLength method.Here is the syntax −public override void SetLength (long value);Here, int64 = Length of the streamValue < current LengthIf the value is less than the current length of the stream: The stream is truncated. If the current position is greater than the new length, the current position is moved to the last byte of the stream.Value > current LengthThe stream is expanded, and the current position remains the same. If the stream is expanded, the contents of the stream between the old and the new length are undefined.The following is an ... Read More

File Objects in C#

George John
Updated on 22-Jun-2020 12:35:19

821 Views

To create a new file in C#, use the FileStream object.The following is the syntax −FileStream = new FileStream( , , , );Let us see an example for a file “test.dat”, which is created/ opened using File object −FileStream F = new FileStream("test.dat", FileMode.OpenOrCreate,FileAccess.ReadWrite);The following is an example −Exampleusing System; using System.IO; namespace FileIOApplication {    class Program {       static void Main(string[] args) {          FileStream F = new FileStream("test.dat", FileMode.OpenOrCreate,          FileAccess.ReadWrite);          for (int i = 1; i

How to inherit a class in C#?

Chandu yadav
Updated on 22-Jun-2020 12:36:17

103 Views

Inheritance allows us to define a class in terms of another class, which makes it easier to create and maintain an application.When creating a class, instead of writing completely new data members and member functions, the programmer can designate that the new class should inherit the members of an existing class. This existing class is called the base class, and the new class is referred to as the derived class. A class can be derived from more than one class or interface, which means that it can inherit data and functions from multiple base classes or interfaces.Let us see an ... Read More

Class and Static Variables in C#

Arjun Thakur
Updated on 22-Jun-2020 12:38:08

8K+ Views

Static variables are used for defining constants because their values can be retrieved by invoking the class without creating an instance of it. Static variables can be initialized outside the member function or class definition. You can also initialize static variables inside the class definition.Example Live Demousing System; namespace StaticVarApplication {    class StaticVar {       public static int num;       public void count() {          num++;       }       public int getNum() {          return num;       }    }    class ... Read More

Conversion of ArrayList to Array in C#

Ankith Reddy
Updated on 22-Jun-2020 12:38:40

945 Views

To convert an ArrayList to Array, use the ToArray() method in C#.Firstly, set an ArrayList −ArrayList arrList = new ArrayList(); arrList.Add("one"); arrList.Add("two"); arrList.Add("three");Now, to convert, use the ToArray() method −arrList.ToArray(typeof(string)) as string[];Let us see the complete code −Example Live Demousing System; using System.Collections; public class Program {    public static void Main() {       ArrayList arrList = new ArrayList();       arrList.Add("one");       arrList.Add("two");       arrList.Add("three");       string[] arr = arrList.ToArray(typeof(string)) as string[];       foreach (string res in arr) {          Console.WriteLine(res);       }    } }Outputone two three

How to implement Traversal in Singly Linked List using C#?

Arjun Thakur
Updated on 22-Jun-2020 12:26:27

374 Views

Set a linkelist collection −var list = new LinkedList();Now, add elements −list.AddLast("One"); list.AddLast("Two"); list.AddLast("Four");Now, let us add new elements in the already created LinkedList −LinkedListNode node = list.Find("Four"); list.AddBefore(node, "Three"); list.AddAfter(node, "Five");Let us now see how to traverse through the nodes in a Singly Linked List −Exampleusing System; using System.Collections.Generic; public class Demo {    public static void Main(string[] args) {       var list = new LinkedList < string > ();       list.AddLast("One");       list.AddLast("Two");       list.AddLast("Four");       Console.WriteLine("Travering...");       foreach(var res in list) {   ... Read More

How to loop through all the elements of an array in C#?

Ankith Reddy
Updated on 22-Jun-2020 12:26:59

183 Views

Firstly, set an array and initialize it −int[] arr = new int[] {34, 56, 12};To loop through all the elements of an array −for (int i = 0; i < arr.Length; i++) {    Console.WriteLine(arr[i]); }Let us see the complete code −Exampleusing System; public class Program {    public static void Main() {       int[] arr = new int[] {34, 56, 12};       // Length       Console.WriteLine("Length:" + arr.Length);       for (int i = 0; i< arr.Length; i++) {          Console.WriteLine(arr[i]);       }    } }

How to List all Substrings in a given String using C#?

George John
Updated on 22-Jun-2020 12:27:41

539 Views

To list all the substrings, use the Substring method and loop through the length of the string.Let’s say our string is −string myStr = "pqrz";Use nested loop and get the substring in a new string −for (int i = 1; i < myStr.Length; i++) {    for (int start = 0; start

How do I identify if a string is a number in C#?

vanithasree
Updated on 02-Apr-2020 11:06:44

242 Views

Let us say our string is −string str = "3456";Now, to check whether the entered string is a number or not −str.All(c => char.IsDigit(c))The above returns true if the string is a number, else false.Here is the complete code −Example Live Demousing System; using System.Linq; namespace Demo {    public class MyApplication {       public static void Main(string[] args) {          string str = "3456";          // checking if string is a number or not          Console.WriteLine(str.All(c => char.IsDigit(c)));       }    } }OutputTrue

Advertisements