Programming Articles - Page 3148 of 3363

What is the base class for all data types in C#.NET?

karthikeya Boyini
Updated on 20-Jun-2020 17:12:27

2K+ Views

Object is the base class for all data types in C#. The Object Type is the ultimate base class for all data types in C# Common Type System (CTS). The object is an alias for System.Object class.When a value type is converted to object type, it is called boxing and on the other hand, when an object type is converted to a value type, it is called unboxing.The following is an example showing the usage of object data types −using System; using System.IO; namespace Demo {    class objectClass {       public int x = 56;   ... Read More

What is the AddRange method in C# lists?

Chandu yadav
Updated on 20-Jun-2020 17:13:12

8K+ Views

AddRange method in lists adds an entire collection of elements. Let us see an example −Firstly, set a list in C# and add elements −List list = new List(); list.Add(100); list.Add(200); list.Add(300); list.Add(400);Now set an array of elements to be added to the list −// array of 4 elements int[] arr = new int[4]; arr[0] = 500; arr[1] = 600; arr[2] = 700; arr[3] = 800;Use the AddRange() method add the entire collection of elements in the list −list.AddRange(arr);Now let us see the complete code and display the list −using System; using System.Collections.Generic; class Demo {    static void ... Read More

What is ternary operator in C#?

Samual Sam
Updated on 20-Jun-2020 16:53:30

371 Views

Ternary operator is a Conditional operator in C#. It takes three arguments and evaluates a Boolean expression.For example −y = (z == 1) ? 100 : 180;Above, if the first operand evaluates to true (1), the second operand is evaluated. If the first operand evaluates to false (0), the third operand is evaluated.The following is an example −Exampleusing System; namespace Demo {    class Program {       static void Main(string[] args) {          int x, y;          x = 25;          y = (x == 25) ? 20 : 30;          Console.WriteLine("Value of x = {0}", y);          y = (x == 1) ? 50 : 90;          Console.WriteLine("Value of y = {0}", y);          Console.ReadLine();       }    } }Above we have two conditions using the ternary operators −y = (x == 25) ? 20 : 30; y = (x == 1) ? 50 : 90;

What are nested namespaces in C#?

Ankith Reddy
Updated on 20-Jun-2020 16:58:33

2K+ Views

A namespace inside a namespace is called a nested namespace in C#. This is mainly done to properly structure your code.We have an outer namespace −namespace outer {}Within that, we have an inner namespace inside the outer namespace −namespace inner {    public class innerClass {       public void display() {          Console.WriteLine("Inner Namespace");       }    } }Now to call the method of inner namespace, set a class object of the inner class and call the method as shown in the below example −namespace outer {    class Program {   ... Read More

What are object data types in C#?

Ankith Reddy
Updated on 20-Jun-2020 17:00:10

1K+ Views

The object types can be assigned values of any other types, value types, reference types, predefined or user-defined types. However, before assigning values, it needs type conversion.The Object Type is the ultimate base class for all data types in C# Common Type System (CTS). Object is an alias for System.Object class.When a value type is converted to object type, it is called boxing and on the other hand, when an object type is converted to a value type, it is called unboxing.The following is an example −object obj; obj = 100; // this is boxingHere is the complete example showing ... Read More

What are objects in C#?

Arjun Thakur
Updated on 20-Jun-2020 17:01:58

1K+ Views

Like any other object-oriented language, C# also has object and classes. Objects are real-world entities and instance of a class. Access the members of the class using an object.To access the class members, you need to use the dot (.) operator after the object name. The dot operator links the name of an object with the name of a member, for example, Box b1 = new Box();Above you can see Box1 is our object. We will use it to access the members −b1.height = 7.0;You can also use it to call member functions −b1.getVolume();The following is an example showing how ... Read More

What are mixed arrays in C#?

Chandu yadav
Updated on 20-Jun-2020 17:02:28

1K+ Views

Mixed arrays are a combination of multi-dimension arrays and jagged arrays.Note − The mixed arrays type is obsolete now since .NET 4.0 update removed it.Let us see how you can declare a mixed array −var x = new object[] {89, 45, "jacob", 9.8}We can also set them as −var x = new object[] {87, 33, "tim", 6.7, new List() {"football", "tennis", "squash", “cricket”} }Since, mixed arrays are obslete now. For the same work, use the List collection. Here, we have set int and string in the list −Tuple tuple = new Tuple(60, "John");The same example is displayed below:using System; using ... Read More

What are file operations in C#?

Ankith Reddy
Updated on 20-Jun-2020 16:44:13

442 Views

C# has the following file operations −Create, open, read and write a file.Append, Delete, etc.The FileStream class in the System.IO namespace helps in reading from, writing to and closing files. This class derives from the abstract class Stream.You need to create a FileStream object to create a new file or open an existing file. The syntax for creating a FileStream object is as follows −FileStream = new FileStream( , , , );Here, the file operations are also included as shown below −The FileMode enumerator defines various methods for opening files. The members of the FileMode enumerator are −Append − It ... Read More

What are integer literals in C#?

Arjun Thakur
Updated on 20-Jun-2020 16:44:54

707 Views

An integer literal can be a decimal, or hexadecimal constant. A prefix specifies the base or radix: 0x or 0X for hexadecimal, and there is no prefix id for decimal. It can also have a suffix that is a combination of U and L, for unsigned and long, respectively.Here are some of the examples of integer literals −200 // int 90u// unsigned intLet’s use the above literal while declaring and initializing a variable −// int int a =200;We will now print the values −Example Live Demousing System; namespace Demo {    class Program {       static ... Read More

Way to read input from console in C#

George John
Updated on 20-Jun-2020 16:45:59

18K+ Views

Use the ReadLine() method to read input from the console in C#. This method receives the input as string, therefore you need to convert it.For example −Let us see how to get user input from user and convert it to integer.Firstly, read user input −string val; Console.Write("Enter integer: "); val = Console.ReadLine();Now convert it to integer −int a = Convert.ToInt32(val); Console.WriteLine("Your input: {0}", a);Let us see this in an example. The input is added using command line argument−Example Live Demousing System; using System.Collections.Generic; class Demo {    static void Main() {       string val;       Console.Write("Enter ... Read More

Advertisements