C# UNIT 3
C# UNIT 3
STRUCT INTERFACE
A structure can also hold constructors, constants, fields, methods, properties, indexers, and
events, etc.
Syntax:
public struct
{
// Fields
// Methods
}
Interface is like a class, it can also have methods, properties, events, and indexers as its
members. But interfaces can only have the declaration of the members. The implementation of
the interface’s members will be given by the class that implements the interface implicitly or
explicitly. Or we can say that it is the blueprint of the class.
Syntax:
interface interface_name
{
// Method Declaration in interface
}
Syntax for struct interface
struct code: interface1, interface2
{
// Method definition for interface method
// Method definition for interface method
}
Example:
using System;
interface interface1
// Declaration of Method
void MyMethod1();
// Interface 2
interface interface2
void MyMethod2();
void MyMethod1()
Console.WriteLine("interface1.Method()");
}
void MyMethod2()
Console.WriteLine("interface2.Method()");
class program{
interface1 M1;
interface2 M2;
// Create objects
M1 = new code();
M2 = new code();
M1.MyMethod1();
M2.MyMethod2();
Output:
interface1.Method() is called
interface2.Method() is called
ENUMS, ENUMERATOR BASE TYPE
enum Level
Low,
Medium,
High
Console.WriteLine(myVar);
ENUM MODIFIERS
In C#, the only modifiers allowed on an enum declaration are access modifiers like "public",
"protected", and "private", which control the visibility of the enum type within your code,
similar to how you would use them with classes.Access modifiers define which parts of your
code can access the enum. Example :
public enum DayOfWeek { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday,
Saturday };
ENUM MEMBERS
The body of an enum type declaration defines zero or more enum members, which are the
named constants of the enum type. No two enum members can have the same name.
Example :
using System;
enum Color
{
Red,
Green = 10,
Blue = 11
}
class Test
{
static void Main()
{
Console.WriteLine(StringFromColor(Color.Red));
Console.WriteLine(StringFromColor(Color.Green));
Console.WriteLine(StringFromColor(Color.Blue));
}
static string StringFromColor(Color c)
{
switch (c)
{
case Color.Red:
return $"Red = {(int) c}";
case Color.Green:
return $"Green = {(int) c}";
case Color.Blue:
return $"Blue = {(int) c}";
default:
return "Invalid color";
}
}
}
Output:
Red = 0
Green = 10
Blue = 11
Each enum type defines a distinct type; an explicit enumeration conversion is required to
convert between an enum type and an integral type, or between two enum types. Any value of
the underlying type of an enum can be cast to the enum type, and is a distinct valid value of
that enum type.
The following operators can be used on values of enum types:
● comparison
● Addition
● Subtraction
● Bitwise
CONCEPT OF ARRAYS
Arrays are used to store multiple values in a single variable, instead of declaring separate
variables for each value.To declare an array, define the variable type with square brackets:
string[] cars;
We have now declared a variable that holds an array of strings.To insert values to it, we can
use an array literal - place the values in a comma-separated list, inside curly braces:
Console.WriteLine(cars[0]);
ARRAY INITIALIZATION
Declaring an array does not initialize the array in the memory. When the array variable is
initialized, you can assign values to the array.Array is a reference type, so you need to use
the new keyword to create an instance of the array. For example,
In C#, the ArrayList is a non-generic collection of objects whose size increases dynamically.
It is the same as Array except that its size increases dynamically.An ArrayList can be used to
add unknown data where you don't know the types and the size of the data.
using System;
using System.Collections;
namespace abc
class Program
arrlist.Add("Welcome");
arrlist.Add("TO");
arrlist.Add("C#");
Console.WriteLine(item);
Console.ReadLine();
2.DELETE : Remove() method removes the specified element from the ArrayList.
using System;
using System.Collections;
namespace abc
{
class Program
arrlist.Add("Welcome");
arrlist.Add("TO");
arrlist.Add("C#");
arrlist.Remove("C#");
Console.WriteLine(item);
Console.ReadLine();
3.SEARCH: Searches for the specified Object and returns the zero-based index of the first
occurrence within the entire ArrayList.
using System;
using System.Collections;
using System.Collections.Generic;
class code {
al.Add("A");
al.Add("B");
al.Add("C");
al.Add("D");
al.Add("E");
if (al.Contains("E"))
else
STRING OPERATIONS
Strings are used for storing text. A string variable contains a collection of characters
surrounded by double quotes. Create a variable of type string and assign it a value:
STRING FUNCTIONS
Function Description
Combines multiple strings into one.
Concat()
Example :
1 . Concat() : string str1 = "Hello, ";
string str2 = "World!";
string result = string.Concat(str1, str2);
Console.WriteLine(txt.ToUpper());
Console.WriteLine(txt.ToLower());
4 . Substring () :
string originalString = "Hello, World!";
string extractedSubstring = originalString.Substring(7);
Console.WriteLine(extractedSubstring);
5. Replace() :
string sentence = "The quick brown fox jumps over the lazy dog";
string newSentence = sentence.Replace("fox", "cat");
Console.WriteLine(newSentence);
In C# object is the main class and it’s also the parent or root of all the classes hence c# is the
object-oriented language. We can convert the object to other data types like string, arrays, etc.
Here we convert the object to a string by using the Object.toString() method for translating
the object to string values.
Example :
using System;
using demo;
namespace demo
{
public class demo1
{
public void exam()
{
Console.WriteLine("Welcome To My Domain its the first example for C# programming
language we want to convert the object to string values");
}
}
}
public class demo2
{
public static void Main()
{
object firstobj = new demo1();
Console.WriteLine(firstobj.ToString());
}
STRING BUILDER
StringBuilder is a Dynamic Object. It doesn’t create a new object in the memory but
dynamically expands the needed memory to accommodate the modified or new
string.A String object is immutable, i.e. a String cannot be changed once created. To avoid
string replacing, appending, removing or inserting new strings in the initial string C#
introduce StringBuilder concept.
StringBuilder.Insert(int index, string This method inserts the string at specified index
value) in StringBuilder object.
Example :
using System;
using System.Text;
class code
s.Append("program");
s.AppendLine("C#");
s.Append("C# program");
Console.WriteLine(s);
Example
The File class has many useful methods for creating and getting information about files. For
example:
Method Description
Replac Replaces the contents of a file with the contents of another file
e()
WriteA Creates a new file and writes the contents to it. If the file already exists, it will be overwritten.
llText()
In the following example, we use the WriteAllText() method to create a file named
"filename.txt" and write some content to it. Then we use the ReadAllText() method to read
the contents of the file:
Example
Hello World!
Here is an example of how to read binary data from a file using BinaryReader:
csharp
Copy
using System;
using System.IO;
class Program
{
static void Main()
{
// File path
string filePath = "example.bin";
1. Endianness: When writing or reading binary data, consider endianness (the byte
order). In most modern systems, little-endian is used, but when dealing with cross-
platform systems or specific hardware, it might differ.
2. Positioning: The BinaryReader and BinaryWriter classes read/write sequentially, so
they keep track of the current position within the file.
3. File Mode: When opening files, you can use different FileMode options (Create,
Open, Append, etc.) to define how the file is opened.