A satellite assembly is a .NET Framework assembly containing resources specific to a given language. Using satellite assemblies, you can place resources for different languages in different assemblies, and the correct assembly is loaded into memory only if the user selects to view the application in that language.
A good example of Satellite assembly implementation can be found here :
http://www.codeproject.com/Articles/352105/Satellite-Assembly-Example-in-Csharp-Step-by-Step
Tuesday, February 12, 2013
Is string a value type or a reference type?What are immutable objects in C#? (C#)
String is a reference type.
An object is immutable if its state doesn’t change once the object has been created. Consequently, a class is immutable if its instances are immutable.
There is one famous immutable class: System.String. When you think that you are modifying a string, you actually create a new string object.
Doing so comes at the cost of creating multiple string objects in memory when doing some intensive string computation. In this case you need to use the System.Text.StringBuilder class that provides a safe way to work with mutable string.
An object is immutable if its state doesn’t change once the object has been created. Consequently, a class is immutable if its instances are immutable.
There is one famous immutable class: System.String. When you think that you are modifying a string, you actually create a new string object.
Doing so comes at the cost of creating multiple string objects in memory when doing some intensive string computation. In this case you need to use the System.Text.StringBuilder class that provides a safe way to work with mutable string.
Boxing and Unboxing (C#)
Boxing is the operation of converting a value of a value type into a value of a corresponding reference type. Boxing in C# is implicit.
Unboxing is the operation of converting a value of a reference type (previously boxed) into a value of a value type. Unboxing in C# requires an explicit type cast. A boxed object of type T can only be unboxed to a T (or a nullable T).
Example:
int foo = 42; // Value type.
object bar = foo; // foo is boxed to bar.
int foo2 = (int)bar; // Unboxed back to value type.
Unboxing is the operation of converting a value of a reference type (previously boxed) into a value of a value type. Unboxing in C# requires an explicit type cast. A boxed object of type T can only be unboxed to a T (or a nullable T).
Example:
int foo = 42; // Value type.
object bar = foo; // foo is boxed to bar.
int foo2 = (int)bar; // Unboxed back to value type.
Monday, February 11, 2013
Value types and Reference types in .Net (C#)
In .NET, there are two categories of types, reference types and value types.
Structs are value types and classes are reference types.
The general difference is that a reference type lives on the heap, and a value type lives inline, that is, where ever it is your variable or field is defined.
A variable containing a value type contains the entire value type value. For a struct, that means that the variable contains the entire struct, with all its fields.
A variable containing a reference type contains a pointer, or a reference to somewhere else in memory where the actual value resides.
This has one benefit, to begin with:
•value types always contains a value
•reference types can contain a null-reference, meaning that they don't refer to anything at all at the moment
Internally, reference types are implemented as pointers, and knowing that, and knowing how variable assignment works, there are other behavioral patterns:
•copying the contents of a value type variable into another variable, copies the entire contents into the new variable, making the two distinct. In other words, after the copy, changes to one won't affect the other
•copying the contents of a reference type variable into another variable, copies the reference, which means you now have two references to the same somewhere else storage of the actual data. In other words, after the copy, changing the data in one reference will appear to affect the other as well, but only because you're really just looking at the same data both places
When you declare variables or fields, here's how the two types differ:
•variable: value type lives on the stack, reference type lives on the stack as a pointer to somewhere in heap memory where the actual memory lives
•class/struct-field: value type lives inside the class, reference type lives inside the class as a pointer to somewhere in heap memory where the actual memory lives.
Structs are value types and classes are reference types.
The general difference is that a reference type lives on the heap, and a value type lives inline, that is, where ever it is your variable or field is defined.
A variable containing a value type contains the entire value type value. For a struct, that means that the variable contains the entire struct, with all its fields.
A variable containing a reference type contains a pointer, or a reference to somewhere else in memory where the actual value resides.
This has one benefit, to begin with:
•value types always contains a value
•reference types can contain a null-reference, meaning that they don't refer to anything at all at the moment
Internally, reference types are implemented as pointers, and knowing that, and knowing how variable assignment works, there are other behavioral patterns:
•copying the contents of a value type variable into another variable, copies the entire contents into the new variable, making the two distinct. In other words, after the copy, changes to one won't affect the other
•copying the contents of a reference type variable into another variable, copies the reference, which means you now have two references to the same somewhere else storage of the actual data. In other words, after the copy, changing the data in one reference will appear to affect the other as well, but only because you're really just looking at the same data both places
When you declare variables or fields, here's how the two types differ:
•variable: value type lives on the stack, reference type lives on the stack as a pointer to somewhere in heap memory where the actual memory lives
•class/struct-field: value type lives inside the class, reference type lives inside the class as a pointer to somewhere in heap memory where the actual memory lives.
What is the difference between a struct and a class? (C#)
Classes Only:
•Can support inheritance
•Are reference (pointer) types
•The reference can be null
•Have memory overhead per new instance
Structs Only:
•Cannot support inheritance
•Are value types
•Are passed by value (like integers)
•Cannot have a null reference (unless Nullable is used)
•Do not have a memory overhead per new instance - unless 'boxed'
Both Classes and Structs:
•Are compound data types typically used to contain a few variables that have some logical relationship
•Can contain methods and events
•Can support interfaces
•Can support inheritance
•Are reference (pointer) types
•The reference can be null
•Have memory overhead per new instance
Structs Only:
•Cannot support inheritance
•Are value types
•Are passed by value (like integers)
•Cannot have a null reference (unless Nullable is used)
•Do not have a memory overhead per new instance - unless 'boxed'
Both Classes and Structs:
•Are compound data types typically used to contain a few variables that have some logical relationship
•Can contain methods and events
•Can support interfaces
Can you overload a C# method by just changing its return type? If no, then why not? (C#)
Overloading is what happens when you have two methods with the same name but different signatures. At compile time, the compiler works out which one it's going to call, based on the compile time types of the arguments and the target of the method call.
For example :
string Substring (int startIndex)
string Substring (int startIndex, int length)
No, a C# method cannot be overloaded by only changing the return type. Overloading happens by changing the parameters. The return type of a method is not considered to be part of a method's signature (section 3.6), and an overload is determined before the compiler checks whether or not the return type will cause an error in the wider context of the method call. In other words, it's not part of the test for an applicable function member.
For example :
string Substring (int startIndex)
string Substring (int startIndex, int length)
No, a C# method cannot be overloaded by only changing the return type. Overloading happens by changing the parameters. The return type of a method is not considered to be part of a method's signature (section 3.6), and an overload is determined before the compiler checks whether or not the return type will cause an error in the wider context of the method call. In other words, it's not part of the test for an applicable function member.
Sunday, February 10, 2013
How do you prevent a class from being inherited? Are there any such classes in the .NET framework? (C#)
In order to prevent a class in C# from being inherited, the keyword sealed is used. Thus a sealed class may not serve as a base class of any other class. It is also obvious that a sealed class cannot be an abstract class. Code below...
//C# Example
sealed class ClassA
{
public int x;
public int y;
}
No class can inherit from ClassA defined above. Instances of ClassA may be created and its members may then be accessed, but nothing like the code below is possible...
class DerivedClass : ClassA { } // Error
Sealed classed in .Net framework are : System.Drawing, System.Collections etc.
//C# Example
sealed class ClassA
{
public int x;
public int y;
}
No class can inherit from ClassA defined above. Instances of ClassA may be created and its members may then be accessed, but nothing like the code below is possible...
class DerivedClass : ClassA { } // Error
Sealed classed in .Net framework are : System.Drawing, System.Collections etc.
Subscribe to:
Posts (Atom)